-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbridge.py
More file actions
61 lines (38 loc) · 980 Bytes
/
bridge.py
File metadata and controls
61 lines (38 loc) · 980 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
"""
桥接模式
"""
from abc import ABCMeta, abstractmethod
class Shape(metaclass=ABCMeta):
def __init__(self, color):
self.__color = color
@abstractmethod
def get_shape_type(self):
pass
def shape_info(self):
print(f"{self.get_shape_type()}是{self.__color.get_color()}色")
class Rectangle(Shape):
def __init__(self, color):
super().__init__(color)
def get_shape_type(self):
return "矩形"
class Ellipse(Shape):
def __init__(self, color):
super().__init__(color)
def get_shape_type(self):
return "椭圆"
class Color(metaclass=ABCMeta):
@abstractmethod
def get_color(self):
pass
class Red(Color):
def get_color(self):
return "红"
class Green(Color):
def get_color(self):
return "绿"
if __name__ == "__main__":
ra = Rectangle(Red())
ra.shape_info()
ea = Ellipse(Green())
ea.shape_info()