forked from amirbigg/python-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge.py
More file actions
53 lines (35 loc) · 1.06 KB
/
bridge.py
File metadata and controls
53 lines (35 loc) · 1.06 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
"""
Bridge
- a structural design pattern that lets you split a large class into two separate
hierarchies — abstraction and implementation — which can be developed independently of each other.
"""
import abc
class Shape(abc.ABC): # Abstraction
def __init__(self, color):
self.color = color
def show(self):
pass
class Circle(Shape): # Refined Abstraction
def show(self):
self.color.paint(self.__class__.__name__)
class Square(Shape): # Refined Abstraction
def show(self):
self.color.paint(self.__class__.__name__)
class Triangle(Shape): # Refined Abstraction
def show(self):
self.color.paint(self.__class__.__name__)
class Color(abc.ABC): # Implementation
def paint(self, name):
pass
class Blue(Color): # Concrete Implementation
def paint(self, name):
print(f'this is a blue {name}')
class Red(Color): # Concrete Implementation
def paint(self, name):
print(f'this is a red {name}')
class Yellow(Color): # Concrete Implementation
def paint(self, name):
print(f'this is a yellow {name}')
ylw = Yellow()
circle = Circle(ylw)
circle.show()