forked from Sean-Bradley/Design-Patterns-In-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposite.py
More file actions
47 lines (30 loc) · 803 Bytes
/
composite.py
File metadata and controls
47 lines (30 loc) · 803 Bytes
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
from abc import ABCMeta, abstractmethod
class IGraphic(metaclass=ABCMeta):
@staticmethod
@abstractmethod
def print():
"""print information"""
class Ellipse(IGraphic):
def print(self):
print("Ellipse")
class Circle(IGraphic):
def print(self):
print("Circle")
class CompositeGraphic(IGraphic):
def __init__(self):
self.child_graphics = []
def add(self, graphic):
self.child_graphics.append(graphic)
def print(self):
for g in self.child_graphics:
g.print()
ELLIPSE1 = Ellipse()
CIRCLE1 = Circle()
COMPOSITE1 = CompositeGraphic()
COMPOSITE1.add(ELLIPSE1)
COMPOSITE2 = CompositeGraphic()
COMPOSITE2.add(CIRCLE1)
COMPOSITE2.add(COMPOSITE1)
COMPOSITE2.print()
# ELLIPSE1.print()
# CIRCLE1.print()