python-Adapter Mode

Source: Internet
Author: User
Tags wrapper

Description

In order to solve the problem of incompatible interfaces the introduction of an interface compatibility mechanism, is the adapter mode, which provides an adapter class to convert the interface provided by a third party to the client's desired interface. Examples of life: mobile phone charger to convert 220v power supply to the appropriate voltage to the phone to charge. This charger is the right fit.

Adapter mode: Transforms the interface of one class into another interface that the customer wants. The adapter mode allows classes that are incompatible with interfaces to work together. The interface referred to in the adapter mode alias for the wrapper (Wrapper) pattern definition refers to the generalized interface, which can represent a collection of methods or methods

Structure of the adapter pattern

The adapter pattern consists of the following 3 roles: Target (object abstract Class) Adapter (Adapter Class) Adaptee (Adaptive Class)

Instance:

Suppose a company A and a company B need to cooperate, company a needs to access company B's personnel information, but company A and the Company B protocol interface is different, what should be handled? The object interfaces are encapsulated by company A and Company B for their respective personnel information access systems first.

classAcpnstaff:"""Company A"""name=""ID=""Phone=""    def __init__(self,id): Self.id=IDdefGetName (self):Print("A Protocol GetName method...id:%s"%self.id)returnSelf.namedefSetName (self,name):Print("A Protocol SetName method...id:%s"%self.id) Self.name=namedefGetphone (self):Print("A Protocol Getphone method...id:%s"%self.id)returnSelf.phonedefSetphone (self,phone):Print("A Protocol Setphone method...id:%s"%self.id) Self.phone=PhoneclassBcpnstaff:"""Company B"""name=""ID=""Telephone=""    def __init__(self,id): Self.id=IDdefget_name (self):Print("B Protocol get_name method...id:%s"%self.id)returnSelf.namedefSet_name (self,name):Print("B Protocol Set_name method...id:%s"%self.id) Self.name=namedefGet_telephone (self):Print("B Protocol Get_telephone method...id:%s"%self.id)returnSelf.telephonedefSet_telephone (self,telephone):Print("B Protocol get_name method...id:%s"%self.id) Self.telephone=TelephoneclassCpnstaffadapter:"""Company C"""B_CPN=""    def __init__(self,id): SELF.B_CPN=Bcpnstaff (ID)defGetName (self):returnSelf.b_cpn.get_name ()defGetphone (self):returnSelf.b_cpn.get_telephone ()defSetName (self,name): Self.b_cpn.set_name (name)defSetphone (Self,phone): Self.b_cpn.set_telephone (phone)#Business Scenariosif __name__=="__main__": Acpn_staff=acpnstaff ("123") Acpn_staff.setname ("x-a") Acpn_staff.setphone ("10012345678")    Print("A Staff name:%s"%acpn_staff.getname ())Print("A Staff phone:%s"%Acpn_staff.getphone ()) Bcpn_staff=cpnstaffadapter ("456") Bcpn_staff.setname ("Y-b") Bcpn_staff.setphone ("99987654321")    Print("B Staff name:%s"%bcpn_staff.getname ())Print("B Staff phone:%s"%bcpn_staff.getphone ())

Printing results:

A Protocol SetName method...id:123
A Protocol Setphone method...id:123
A Protocol GetName method...id:123
A Staff Name:x-a
A Protocol Getphone method...id:123
A Staff phone:10012345678
B Protocol Set_name method...id:456
B Protocol Get_name method...id:456
B Protocol Get_name method...id:456
B Staff Name:y-b
B Protocol Get_telephone method...id:456
B Staff phone:99987654321

It can be seen that class A and the internal methods of Class B are different, that is, the use of a consistent interface to A/b company to access simultaneously, then the C class was born. The C class plays the role of the adapter of the Protocol and Interface transformation, encapsulates the B Company personnel interface, and the external interface form is consistent with the company a personnel interface, achieves the effect of Access B Company personnel information with Company a personnel interface.

Pattern Benefits

Decoupling the target class and the adapter class, by introducing an adapter class to reuse the existing adaptive class, without modifying the original structure to increase the transparency and reusability of the class, improve the reusability of the adapter, the same adapter class can be reused in many different systems flexibility and scalability is very good Class Adapter Mode: The method of displacing some adapters is convenient for object adapter mode: Multiple different adapters can be adapted to the same target, and a subclass of an adapter can be fitted

Mode disadvantage class Adapter mode:

(1) A maximum of one adapter class can be adapted at a time, can not be adapted to a plurality of adapters; (2) The adapter class cannot be the final class, and (3) the target abstract class can only be an interface and cannot be a class object adapter pattern: Some methods of displacing the adapter class in adapters are troublesome

Mode applicable environment

The system needs to use some existing classes, and the interfaces of these classes do not meet the needs of the system, or even the source code of these classes can be used to create a reusable class, and some classes that are not too much related to each other, including some may be introduced in the future to work with the class

Other than that:

Adapter mode and decorative mode have a certain similarity, all play the role of packaging, but they are different in nature, the result of decoration mode is to give an object to add some additional responsibilities, and the adapter mode is the other object is "disguised." Adapters can be considered a compensating application for today's business, so try not to use the adapter mode in the design phase, and consider using adapter mode when two systems need to be compatible.

python-Adapter Mode

Related Article

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.