III.结构模式
1.代理模式
-
代理模式,一个类代表另一个类功能,在代理模式中,我们创建具有现有对象的对象,以便向外界提供功能接口。
class SensitiveInfo: def __init__(self): self.users = ["nick","tom","ben","mike"] def read(self): print("the username is {}".format(' '.join(self.users))) def add(self,user): self.users.append(user) print("add user {}".format(user)) class Info: def __init__(self): self.protected = SensitiveInfo() self.secret = "123456" def read(self): self.protected.read() def add(self, user): sec = input('what is the secret? ') self.protected.add(user) if sec == self.secret else print("That's wrong!") def main(): info = Info() while True: print('1. read list |==| 2. add user |==| 3. quit') key = input('choose option: ') if key == '1': info.read() elif key == '2': name = input('choose username: ') info.add(name) elif key == '3': exit() else: print('unknown option: {}'.format(key)) if __name__ == '__main__': main()
2.外观模式
- 隐藏系统复杂性,向客户端提供一个客户端可以访问系统接口。向现有系统添加一个接口,来隐藏系统复杂性。这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。
class A:
def run(self):
print('A run')
def jump(self):
print('A jump')
class B:
def run(self):
print('B run')
def jump(self):
print('B jump')
class C:
def run(self):
print('C run')
def jump(self):
print('C jump')
class Facade:
def __init__(self):
self.a = A()
self.b = B()
self.c = C()
def run(self):
for item in ("a","b","c"):
obj = getattr(self,item)
obj.run()
def jump(self):
for item in ("a","b","c"):
getattr(self,item).jump()
if __name__ == '__main__':
facade = Facade()
facade.run()
facade.jump()
3.桥接模式
- 在python程序设计中,桥接指的是抽象部分和实体部分的连接,简单来说是类和类实例化过程中链接。
class A:
def run(self,name):
print("my name is {}".format(name))
class B:
def run(self, name):
print("我的名字是:{}".format(name))
class Bridge:
def __init__(self, ager, classname):
self.ager = ager
self.classname = classname
def bridge_run(self):
self.classname.run(self.ager)
if __name__ == '__main__':
test = Bridge('Tom', A())
test.bridge_run()
4.组合模式
- 用于把一组相似对象当作一个单一的对象,组合模式依据树形结构来组合对象。您想表示对象的部分-整体层次结构(树形结构)。 2、您希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。
class Company:
name = ''
def __init__(self, name):
self.name = name
def add(self, company):
pass
def remove(self, company):
pass
def display(self, depth):
pass
def line_of_duty(self): # 履行职责
pass
# Composite:公司类
class ConcreteCompany(Company):
childrenCompany = None
def __init__(self, name):
Company.__init__(self, name)
self.childrenCompany = []
def add(self, company):
self.childrenCompany.append(company)
def remove(self, company):
self.childrenCompany.remove(company)
def display(self, depth):
print('-' * depth + self.name)
for component in self.childrenCompany:
component.display(depth + 2)
def line_of_duty(self): # 履行职责
for component in self.childrenCompany:
component.line_of_duty()
# Leaf:具体职能部门
class HRDepartment(Company):
def __init__(self, name):
Company.__init__(self, name)
def display(self, depth):
print('-' * depth + self.name)
def line_of_duty(self): # 履行职责
print('%s 员工招聘培训管理' % self.name)
# Leaf:具体职能部门
class FinanceDepartment(Company):
def __init__(self, name):
Company.__init__(self, name)
def display(self, depth):
print('-' * depth + self.name)
def line_of_duty(self): # 履行职责
print('%s 公司财务收支管理' % self.name)
if __name__ == '__main__':
root = ConcreteCompany('北京总公司')
root.add(HRDepartment('总公司人力资源部'))
root.add(FinanceDepartment('总公司财务部'))
comp = ConcreteCompany('华东分公司')
comp.add(HRDepartment('华东分公司人力资源部'))
comp.add(FinanceDepartment('华东分公司财务部'))
root.add(comp)
comp1 = ConcreteCompany('南京办事处')
comp1.add(HRDepartment('南京办事处人力资源部'))
comp1.add(FinanceDepartment('南京办事处财务部'))
comp.add(comp1)
comp2 = ConcreteCompany('杭州办事处')
comp2.add(HRDepartment('杭州办事处人力资源部'))
comp2.add(FinanceDepartment('杭州办事处财务部'))
comp.add(comp2)
print('-------公司结构图-------')
root.display(1)
print('
-------职责-------')
root.line_of_duty()
5.装饰者模式--函数装饰器
import time
from functools import wraps
def timethis(func):
'''
Decorator that reports the execution time.
'''
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(func.__name__, end - start)
return result
return wrapper
@timethis
def fun():
time.sleep(3)
return 1
if __name__ == '__main__':
fun()
6.装饰者模式--类装饰器
class Spam:
@profiled
def bar(self,x)
print(self,x)
- 我们想给一个对象添加额外功能,可以通过装饰器,继承或是组合方式,相比继承,它会使代码更难去复用,继承关系是静态的。
# 修饰器装饰
class Foo:
def f1(self):
print("original f1")
def f2(self):
print("original f2")
class Foo_decorator:
def __init__(self, decoratee):
self._decoratee = decoratee
def f1(self):
print("before run f1")
self._decoratee.f1()
print("after run f1")
def __getattr__(self, name):
return getattr(self._decoratee, name)
if __name__ == '__main__':
u = Foo()
v = Foo_decorator(u)
v.f1()
v.f2()
7.适配器模式
- 做为两个接口是否兼容的桥梁,这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。举个真实的例子,读卡器是作为内存卡和笔记本之间的适配器。您将内存卡插入读卡器,再将读卡器插入笔记本,这样就可以通过笔记本来读取内存卡。
class Dog:
def __init__(self, name):
self.name = name
def wangwang(self):
print('my name is' + self.name + '。。。汪汪汪。。。')
def dog_run(self):
print(f'{self.name} is running')
class Cat:
def __init__(self, name):
self.name = name
def miaomiao(self):
print('my name is' + self.name + '。。。喵喵喵。。。')
def cat_run(self):
print(f'{self.name} is running')
class Sheep:
def __init__(self, name):
self.name = name
def miemie(self):
print('my name is' + self.name + '。。。咩咩。。。')
def sheet_run(self):
print(f'{self.name} is running')
class Adapter:
def __init__(self, adapted_methods):
self.__dict__.update(adapted_methods)
def speak(self):
pass
def run(self):
pass
def main():
animals = []
dog = Dog('旺财')
cat = Cat('大脸猫')
sheep = Sheep('喜洋洋')
animals.append(Adapter({'speak': dog.wangwang, 'run': dog.dog_run}))
animals.append(Adapter({'speak': cat.miaomiao, 'run': cat.cat_run}))
animals.append(Adapter({'speak': sheep.miemie, 'run': sheep.sheet_run}))
for a in animals:
a.speak()
a.run()
if __name__ == '__main__':
main()