-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_bridge.py
71 lines (44 loc) · 1.14 KB
/
4_bridge.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
"""
桥模式:让相关类解耦合
Line -- RedLine 缺点
不同形状,不同颜色
"""
from abc import abstractmethod, ABCMeta
# 抽象接口 Shape,Color
class Shape(metaclass=ABCMeta):
@abstractmethod
def draw(self):
pass
class Color(metaclass=ABCMeta):
@abstractmethod
def paint(self):
pass
# 具体实现形状: Line Rectangle
class Line(Shape):
name = "直线"
def __init__(self):
self.draw()
def draw(self):
print("---画出一条直线---")
class Rectangle(Shape):
name = "长方形"
def __init__(self):
self.draw()
def draw(self):
print("---画出一个长方形---")
class Red(Color):
def __init__(self, shape):
self.shape = shape
def paint(self):
print("红色的%s"%self.shape.name)
class Green(Color):
def __init__(self, shape):
self.shape = shape
def paint(self):
print("绿色的%s" % self.shape.name)
if __name__ == '__main__':
user_red_line = Red(Line())
user_red_line.paint()
print('---'*10)
user_green_rectangle = Green(Rectangle())
user_green_rectangle.paint()