Created
October 19, 2023 16:01
-
-
Save akbariandev/c919aff1dc55a45ac830827e28a48421 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub trait Factory { | |
fn name(&self) -> String; | |
} | |
pub struct CarFactory { | |
name: String | |
} | |
impl CarFactory { | |
pub fn car(name: &str) -> CarFactory{ | |
CarFactory{ | |
name: String::from(name) | |
} | |
} | |
} | |
impl Factory for CarFactory { | |
fn name(&self) -> String { | |
self.name.to_string() | |
} | |
} | |
pub struct CellPhone { | |
name: String | |
} | |
impl CellPhone { | |
pub fn cellphone(name: &str) -> CellPhone { | |
CellPhone { | |
name: String::from(name) | |
} | |
} | |
} | |
impl Factory for CellPhone { | |
fn name(&self) -> String { | |
self.name.to_string() | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use crate::{CarFactory, CellPhone, Factory}; | |
#[test] | |
fn it_works() { | |
let car = CarFactory::car("Dodge Challenger"); | |
assert_eq!(car.name(), "Dodge Challenger"); | |
let phone = CellPhone::cellphone("iPhone 15"); | |
assert_eq!(phone.name(), "iPhone 15"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment