设计模式--适配器

做用:适配器模式能够将不统一的接口统一到同一种形式,方便管理。
应用场景:根据一种需求写的各类类,定义好各个函数接口,可能被其余后加功能体系征用
,产>生函数不统一的现象,这时候就能够用适配器模式进行归一。目的是保证数据源,功能 > 的统一
class Adaptee:

    def specific_request(self):

        return 'Adaptee'



class Adapter:

    def __init__(self, adaptee):

        self.adaptee = adaptee



    def request(self):

        return self.adaptee.specific_request()



client = Adapter(Adaptee())

print(client.request())



# --------- Second example (by Alex Martelli)------------



class UppercasingFile:

    def __init__(self, *a, **k):

        self.f = file(*a, **k)

    def write(self, data):

        self.f.write(data.upper())

    def __getattr__(self, name):

        return getattr(self.f, name)
相关文章
相关标签/搜索