-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
105 lines (85 loc) · 2.27 KB
/
lib.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// We don't use this reference to self here...
trait FlyBehavior {
fn fly(&self) -> ();
}
trait QuackBehavior {
fn quack(&self) -> ();
}
struct Duck {
fly_behavior: Box<dyn FlyBehavior>,
quack_behavior: Box<dyn QuackBehavior>,
display: fn() -> (),
}
impl Duck {
pub fn perform_fly(&self) {
self.fly_behavior.fly();
}
pub fn perform_quack(&self) {
self.quack_behavior.quack();
}
pub fn swim(&self) {
println!("All ducks float, even decoys!");
}
pub fn set_fly_beavior(&mut self, fb: Box<dyn FlyBehavior>) {
self.fly_behavior = fb;
}
pub fn set_quack_beavior(&mut self, qb: Box<dyn QuackBehavior>) {
self.quack_behavior = qb;
}
}
struct FlyWithWings {}
impl FlyBehavior for FlyWithWings {
fn fly(&self) {
println!("I am flying!");
}
}
struct FlyNoWay {}
impl FlyBehavior for FlyNoWay {
fn fly(&self) {
println!("I can't fly.");
}
}
struct Quack {}
impl QuackBehavior for Quack {
fn quack(&self) {
println!("Quack!");
}
}
struct MuteQuack {}
impl QuackBehavior for MuteQuack {
fn quack(&self) {
println!("<< Silence >>");
}
}
struct Squeak {}
impl QuackBehavior for Squeak {
fn quack(&self) {
println!("Squeak!");
}
}
pub fn strategy_pattern() {
let qb = Box::new(Quack {});
let fb = Box::new(FlyWithWings {});
// This doesn't achieve the same thing as in the Typescript. This requires
// me to spin up a new mallar duck with these type not assigned to that
// super type.
//
// However, we are able to achieve the dynamic dispatch here. The
// extension of the duck class to create the subclass to come.
let mut mallard_duck = Duck {
fly_behavior: fb,
quack_behavior: qb,
display: || { println!("here I am!"); }
};
// TODO I don't want to call this like this...
(mallard_duck.display);
mallard_duck.swim();
mallard_duck.perform_fly();
mallard_duck.perform_quack();
mallard_duck.set_fly_beavior(Box::new(FlyNoWay {}));
mallard_duck.set_quack_beavior(Box::new(Squeak {}));
mallard_duck.perform_fly();
mallard_duck.perform_quack();
mallard_duck.set_quack_beavior(Box::new(MuteQuack {}));
mallard_duck.perform_quack();
}