-
Notifications
You must be signed in to change notification settings - Fork 2
/
factory.rs
44 lines (37 loc) · 1.16 KB
/
factory.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
///Trait contract of the feature we want to implement
pub trait Animal {
fn speak(&self);
}
/// Data type of the implementation of trait
pub struct Human;
/// Implementation of the trait
impl Animal for Human{
fn speak(&self) {
println!("Hello there")
}
}
/// Trait contract of the factory
pub trait Factory {
fn build_animal(&self) -> Box<dyn Animal>;
}
/// Factory type to be implemented
pub struct AnimalFactory;
/// Implementation of the factory for a specific type, but without specify that type to the
/// consumer of the factory. We hide that implementation using [Box<dyn Animal>]
/// So now potentially we can refactor the implementation type of [Animal] in the factory,
/// and the client using the factory wont notice any differences.
impl Factory for AnimalFactory {
fn build_animal(&self) -> Box<dyn Animal> {
Box::new(Human)
}
}
#[cfg(test)]
mod tests {
use crate::creational::factory::{AnimalFactory, Factory};
#[test]
fn factory_pattern() {
let animal_factory:Box<dyn Factory> = Box::new(AnimalFactory);
let animal = animal_factory.build_animal();
println!("${:?}", animal.speak());
}
}