Skip to content

Instantly share code, notes, and snippets.

@akbariandev
Created October 19, 2023 16:01
Show Gist options
  • Save akbariandev/c919aff1dc55a45ac830827e28a48421 to your computer and use it in GitHub Desktop.
Save akbariandev/c919aff1dc55a45ac830827e28a48421 to your computer and use it in GitHub Desktop.
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