-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathduck.py
131 lines (87 loc) · 2.54 KB
/
duck.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class FlyBehavior:
def fly(self):
raise NotImplementedError
class FlyWithWings(FlyBehavior):
def fly(self):
print("I'm flying!!")
class FlyNoWay(FlyBehavior):
def fly(self):
print("I can't fly")
class FlyRocketPowered(FlyBehavior):
def fly(self):
print("I'm flying with a rocket!'")
class QuackBehavior:
def quack(self):
raise NotImplementedError
class Quack(QuackBehavior):
def quack(self):
print("Quack")
class MuteQuack(QuackBehavior):
def quack(self):
print("<< Silence >>")
class Squeak(QuackBehavior):
def quack(self):
print("Squeak")
class Duck:
_fly_behavior = None
_quack_behavior = None
@property
def fly_behavior(self):
return self._fly_behavior
@fly_behavior.setter
def fly_behavior(self, fly_behavior):
self._fly_behavior = fly_behavior
@property
def quack_behavior(self):
return self._quack_behavior
@quack_behavior.setter
def quack_behavior(self, quack_behavior):
self._quack_behavior = quack_behavior
def display(self):
raise NotImplementedError
def perform_fly(self):
self.fly_behavior.fly()
def perform_quack(self):
self.quack_behavior.quack()
def swim(self):
print("All ducks float, even decoys!")
class MallardDuck(Duck):
def __init__(self):
self.fly_behavior = FlyWithWings()
self.quack_behavior = Quack()
def display(self):
print("I'm a real Mallard duck")
class DecoyDuck(Duck):
def __init__(self):
self.fly_behavior = FlyNoWay()
self.quack_behavior = MuteQuack()
def display(self):
print("I'm a duck Decoy")
class ModelDuck(Duck):
def __init__(self):
self.fly_behavior = FlyNoWay()
self.quack_behavior = Squeak()
def display(self):
print("I'm a real Mallard duck")
class RedHeadDuck(Duck):
def __init__(self):
self.fly_behavior = FlyWithWings()
self.quack_behavior = Quack()
def display(self):
print("I'm a real Red Headed duck")
class RubberDuck(Duck):
def __init__(self):
self.fly_behavior = FlyNoWay()
self.quack_behavior = Squeak()
def display(self):
print("I'm a rubber duckie")
def mini_duck_simulator():
mallard = MallardDuck()
mallard.perform_quack()
mallard.perform_fly()
model = ModelDuck()
model.perform_fly()
model.fly_behavior = FlyRocketPowered()
model.perform_fly()
if __name__ == "__main__":
mini_duck_simulator()