forked from wklken/py-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.py
60 lines (39 loc) · 1.09 KB
/
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
#!/usr/bin/env python
# encoding: utf-8
"""
桥接模式
将抽象部分与它的实现部分分离, 使它们都可以独立地变化
- 独立变化
"""
from abc import ABCMeta, abstractmethod
class Implementor(object):
__metaclass__ = ABCMeta
@abstractmethod
def operation(self):
pass
class ConcreteImplementorA(Implementor):
def operation(self):
print "plan A"
class ConcreteImplementorB(Implementor):
def operation(self):
print "plan B"
class Abstraction(object):
def __init__(self, implementor=None):
if implementor is not None:
self.__implementor = implementor
@property
def implementor(self):
return self.__implementor
@implementor.setter
def implementor(self, value):
self.__implementor = value
def operation(self):
self.__implementor.operation()
class RefinedAbstraction(Abstraction):
pass
if __name__ == '__main__':
ab = RefinedAbstraction()
ab.implementor = ConcreteImplementorA()
ab.operation()
ab.implementor = ConcreteImplementorB()
ab.operation()