-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpattern_decorator_3.py
134 lines (93 loc) · 2.14 KB
/
pattern_decorator_3.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
132
133
134
from abc import ABC, abstractmethod
class Creature(ABC):
@abstractmethod
def feed(self):
pass
@abstractmethod
def move(self):
pass
@abstractmethod
def make_noise(self):
pass
class Animal(Creature):
def feed(self):
print('I eat grass')
def move(self):
print('I walk forward')
def make_noise(self):
print('WOOOO!')
def __str__(self):
return 'Eto eniaml'
class AbstractDeocrator(Creature):
def __init__(self, obj):
self.obj = obj
def feed(self):
self.obj.feed()
def move(self):
self.obj.move()
def make_noise(self):
self.obj.make_noise()
class Swimming(AbstractDeocrator):
def move(self):
print('I swim')
def make_noise(self):
print('...')
class Predator(AbstractDeocrator):
def feed(self):
print('I eat other animals')
class Fast(AbstractDeocrator):
def move(self):
self.obj.move()
print('Fast!')
def __str__(self):
return 'ETO FAST ANIMAL, PIZDES'
def main():
animal = Animal()
animal.feed()
animal.move()
animal.make_noise()
print()
swimming = Swimming(animal)
swimming.feed()
swimming.move()
swimming.make_noise()
print()
predator = Predator(animal)
predator.feed()
predator.move()
predator.make_noise()
print()
fast = Fast(animal)
fast.feed()
fast.move()
fast.make_noise()
faster = Fast(animal)
faster.feed()
faster.move()
faster.make_noise()
print()
print(faster.obj)
#print(faster.obj.obj)
#faster.base.base = faster.base.base.base
#faster.feed()
#faster.move()
#faster.make_noise()
def my_main():
my_animal = Animal()
my_animal.move()
my_animal.feed()
my_animal.make_noise()
print()
my_fast = Fast(my_animal)
my_fast.move()
my_fast.feed()
my_fast.make_noise()
print()
my_new_fast = Fast(my_fast)
my_new_fast.move()
my_new_fast.feed()
my_new_fast.make_noise()
print(my_new_fast.obj)
print(my_new_fast.obj.obj)
if __name__ == '__main__':
main()