1. 适配器模式(Adapter)介绍
UML类图
适配器模式(Adapter Pattern):将一个类的接口转换成为客户希望的另外一个接口,Adapter Pattern使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
应用场景
系统数据和行为都正确,但接口不符合时,目的是使控制范围之外的一个原有对象与某个接口匹配,适配器模式主要应用于希望复用一些现存的类,但接口又与复用环境不一致的情况。
2. 示例
'''Adapter Pattern with Python Code
'''
class Target(object):
def request(self):
print("general request")
class Adaptee(object):
def specific_request(self):
print("specific request")
class Adapter(Target):
def __init__(self):
self.adaptee = Adaptee()
def request(self):
self.adaptee.specific_request()
class Client(object):
def main(self):
target = Adapter()
target.request()
if __name__ == "__main__":
Client().main()
输出:
# ./Adapter.py
specific request
参考:
https://www.cnblogs.com/onepiece-andy/p/python-adapter-pattern.html
https://www.cnblogs.com/milicool/p/11278617.html
https://www.runoob.com/design-pattern/adapter-pattern.html