forked from tuvo1106/python_design_patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassic.py
More file actions
58 lines (39 loc) · 1.29 KB
/
classic.py
File metadata and controls
58 lines (39 loc) · 1.29 KB
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
from abc import ABC
class Shape(ABC):
def __str__(self):
return ''
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def resize(self, factor):
self.radius *= factor
def __str__(self):
return f'A circle of radius {self.radius}'
class Square(Shape):
def __init__(self, side):
self.side = side
def __str__(self):
return f'A square with side {self.side}'
class ColoredShape(Shape):
def __init__(self, shape, color):
if isinstance(shape, ColoredShape):
raise Exception('Cannot apply same decorator twice')
self.shape = shape
self.color = color
def __str__(self):
return f'{self.shape} has the color {self.color}'
class TransparentShape(Shape):
def __init__(self, shape, transparency):
self.shape = shape
self.transparency = transparency
def __str__(self):
return f'{self.shape} has {self.transparency * 100}% transparency'
if __name__ == "__main__":
CIRCLE = Circle(2)
print(CIRCLE)
RED_CIRCLE = ColoredShape(CIRCLE, 'red')
print(RED_CIRCLE)
RED_HALF_TRANS_CIRCLE = TransparentShape(RED_CIRCLE, 0.5)
print(RED_HALF_TRANS_CIRCLE)
MIXED = ColoredShape(ColoredShape(CIRCLE, 'red'), 'green')
print(MIXED)