[Python design mode] 17th translation officer in the program--Adapter mode

Source: Internet
Author: User

GitHub Address: Https://github.com/cheesezh/python_design_patterns

Adapter mode

Adapter mode, which transforms the interface of a class into another interface that the customer wants. The adapter mode makes it possible to work with those classes that would otherwise not work together because of incompatible interfaces [DP].

When the data and behavior of the system are correct, but the interface does not match, we should consider using the adapter mode, which is to match an existing object outside the control range with an interface. The adapter pattern is primarily used in situations where you want to reuse some existing classes, but the interfaces are inconsistent with the reuse environment requirements.

class Target():    """    Target类,这是客户所期待的接口。可以是具体或抽象的类,也可以是接口。    """    def request(self):        print("普通请求")                class Adaptee():    """    需要适配的类    """    def specific_request(self):        print("特殊请求")                class Adapter(Target):    """    适配器,通过内部包装一个Adaptee对象,把源接口转换成目标接口    """    def __init__(self):        self.adaptee = Adaptee()            def request(self):        self.adaptee.specific_request()                def main():    target = Adapter()    target.request()    main()
特殊请求
When to use adapter mode?

Want to use an already existing class, but if its interface, that is, its methods and your requirements do not want the same time, you should consider the use of adapter mode.

For systems that are independently developed within the company, the specification of class and method names should be specified at the beginning of the design, and when the interfaces are not the same, the adapter should not be considered first, but should be considered by refactoring the unified interface.

Use the adapter mode only when both sides are not easy to modify.

But at the beginning of the design, we are prepared to use third-party development components, and the interface of this component is not the same as our own system interface, and we have absolutely no need to change their interface in order to cater to it, although in the design phase of development, that is, you can consider the adapter mode to solve the different interface problems.

Topic

The

uses the program to simulate Yao Ming to foreign play in the NBA initially rely on translation scenarios.

From ABC import Abcmeta, Abstractmethodclass Player (): __metaclass__ = Abcmeta def __init__ (self, name): Self.name = name @abstractmethod def attack (self): pass @abstractmethod def defense (sel f): Pass class forwards (Player): Def attack (self): print ("Forward {} attack". Format (Self.name)        ) def defense (self): print ("Forward {} defense". Format (self.name)) class guards (Player): Def attack (self): print ("Guards {} attack". Format (self.name)) def defense (self): print ("Gu             Ards {} defense ". Format (self.name)) class Foreigncenter (): Def __init__ (self, name): Self.name = Name def jingong (self): print ("Center {} jingong". Format (self.name)) def Fangshou (self): p Rint ("Center {} Fangshou". Format (self.name)) class Translator (Player): Def __init__ (self, name): Self.foreign _center = Foreigncenter (Name) def attack (self): Self.foreign_center.jingong () def Defense (self): Self.foreign_  Center.fangshou () forwards = forwards ("FFF") forwards.attack () Guards = Guards ("GGG") guards.defense () center = Translator ("CCC") Center.attack () Center.defense ()
Forward FFF attackGuards GGG defenseCenter CCC jingongCenter CCC fangshou

[Python design mode] 17th translation officer in the program--Adapter mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.