python 设计模式-适配器模式

Python 设计模式–适配器模式

Demo

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

import abc

class Target(metaclass=abc.ABCMeta):
"""
Define the domain-specific interface that Client uses.
"""

def __init__(self):
self._adaptee = Adaptee()

@abc.abstractmethod
def request(self):
pass


class Adapter(Target):
"""
Adapt the interface of Adaptee to the Target interface.
"""

def request(self):
self._adaptee.specific_request()


class Adaptee:
"""
Define an existing interface that needs adapting.
"""

def specific_request(self):
pass


def main():
adapter = Adapter()
adapter.request()


if __name__ == "__main__":
main()