forked from mlyangyue/design_patterns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bridge_pattern.py
65 lines (46 loc) · 1.26 KB
/
bridge_pattern.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Andy'
"""
大话设计模式
设计模式——桥接模式
桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使它们都可以独立地变化.
程序实例:手机软件的分类 和 手机品牌的分类 耦合度低 两种分类中间用一个set_handsetsoft桥接,各自的变化,不影响其他分类
"""
# 抽象手机软件类
class HandsetSoft(object):
def run(self):
pass
#具体游戏类,游戏是手机软件,继承抽象手机软件类
class HandsetGame(HandsetSoft):
def run(self):
print "运行手机游戏"
#手机通讯录
class HandsetAddressList(HandsetSoft):
def run(self):
print "运行通信录"
#抽象手机品牌类
class HandsetBrand(object):
def __init__(self):
self.soft = ""
def set_handsetsoft(self,soft):
self.soft = soft
def run(self):
pass
# 手机品牌N
class HandsetBrandN(HandsetBrand):
def run(self):
self.soft.run()
# 手机品牌M
class HandsetBrandM(HandsetBrand):
def run(self):
self.soft.run()
if __name__ == "__main__":
game = HandsetGame()
address = HandsetAddressList()
phoneN = HandsetBrandN()
phoneN.set_handsetsoft(game)
phoneN.run()
phoneM = HandsetBrandM()
phoneM.set_handsetsoft(address)
phoneM.run()