Skip to content

Commit 6ab1d6a

Browse files
committed
Structural pattern => Composite : struct of tree
1 parent 7e6309c commit 6ab1d6a

2 files changed

Lines changed: 136 additions & 2 deletions

File tree

composit.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
"""
2+
structural:
3+
Composite
4+
Example:
5+
when want to set tree
6+
"""
7+
8+
from abc import ABC, abstractmethod
9+
10+
11+
class World(ABC):
12+
@abstractmethod
13+
def show(self): pass
14+
15+
16+
class Being(World):
17+
def __init__(self, name):
18+
self.name = name
19+
self.children = []
20+
21+
def add(self, object):
22+
self.children.append(object)
23+
24+
def remove(self, object):
25+
self.children.remove(object)
26+
27+
def show(self):
28+
print(f'Being Composite {self.name}')
29+
for child in self.children:
30+
child.show()
31+
32+
33+
class Animal(World):
34+
def __init__(self, name):
35+
self.name = name
36+
self.children = []
37+
38+
def add(self, object):
39+
self.children.append(object)
40+
41+
def remove(self, object):
42+
self.children.remove(object)
43+
44+
def show(self):
45+
print(f'\tAnimal Composite {self.name}')
46+
for child in self.children:
47+
child.show()
48+
49+
50+
class Human(World):
51+
def __init__(self, name):
52+
self.name = name
53+
self.children = []
54+
55+
def add(self, object):
56+
self.children.append(object)
57+
58+
def remove(self, object):
59+
self.children.remove(object)
60+
61+
def show(self):
62+
print(f'\tHuman Composite {self.name}')
63+
for child in self.children:
64+
child.show()
65+
66+
67+
class Cat(World):
68+
def __init__(self, name):
69+
self.name = name
70+
71+
def show(self):
72+
print(f'\t\tCat Leaf {self.name}')
73+
74+
75+
class Dog(World):
76+
def __init__(self, name):
77+
self.name = name
78+
79+
def show(self):
80+
print(f'\t\tDog Leaf {self.name}')
81+
82+
83+
class Male(World):
84+
def __init__(self, name):
85+
self.name = name
86+
87+
def show(self):
88+
print(f'\t\tMale Leaf {self.name}')
89+
90+
91+
class Female(World):
92+
def __init__(self, name):
93+
self.name = name
94+
95+
def show(self):
96+
print(f'\t\tFemale Leaf {self.name}')
97+
98+
99+
if __name__ == '__main__':
100+
101+
cat = Cat('Missy')
102+
dog = Dog('Jack')
103+
104+
male = Male('Mark')
105+
female = Female('Jane')
106+
107+
animal = Animal('animal1')
108+
human = Human('human1')
109+
110+
animal.add(cat)
111+
animal.add(dog)
112+
113+
human.add(male)
114+
human.add(female)
115+
116+
being = Being('all')
117+
being.add(animal)
118+
being.add(human)
119+
120+
being.show()
121+
122+
# Being Composite all
123+
# Animal Composite animal
124+
# Animal Leaf Missy
125+
# Animal Leaf Jack
126+
# Human Composite human
127+
# Human Leaf Mark
128+
# Human Leaf Jane

template_method.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
Behavioral pattern:
3+
Template method
4+
Example:
5+
when we have static job between several classes use one ABC class
6+
"""
17
from abc import ABC, abstractmethod
28

39

@@ -46,8 +52,8 @@ def fourth_require(self):
4652
print('This is Fourth require from Two...')
4753

4854

49-
def client(klass):
50-
klass.template_method()
55+
def client(class_):
56+
class_.template_method()
5157

5258

5359
if __name__ == '__main__':

0 commit comments

Comments
 (0)