I understand the design pattern (C ++ implementation) -- adapter Pattern)

Source: Internet
Author: User
Solved problems:

The adapter mode converts an interface of a class into another interface that the client expects, so that the two classes that the original interface does not match and cannot work together can work together. For example, for my HP notebook and American products, the voltage in the United States is 220 V, and the voltage in China is V, which can be used in China, you must find a transformer to change the voltage. This transformer is an adapter.

The adapter mode can be a class adapter or an Object Adapter, which we will discuss separately.

Class adapter:

The figure shows that the adaptee class does not have a request method, and the customer expects this method. To enable the customer to use the adaptee class and provide an intermediate link, that is, the class adapter class, the adapter class implements the Target Interface and inherits from adaptee, the request method of the adapter Class re-encapsulates the specificrequest method of adaptee to achieve adaptation.

Because the adapter and adaptee are inherited, this determines that the adapter mode is a class.

The role involved in this adapter mode includes:

Target role: This is the interface the customer expects. Because C # does not support multi-inheritance, the target must be an interface, not a class.
Source (adaptee) role: the class to be adapted.
Adapter role: converts the source interface to the target interface. This role must be a class

Simple implementation:

#include<iostream>using namespace std;// "ITarget"class Target{public:// Methodsvirtual void Request(){};};// "Adaptee"class Adaptee{public:// Methodsvoid SpecificRequest(){cout<<"Called SpecificRequest()"<<endl;}};// "Adapter"class Adapter : public Adaptee, public Target{public:// Implements ITarget interfacevoid Request(){// Possibly do some data manipulation// and then call SpecificRequest  this->SpecificRequest();}};int main(){// Create adapter and place a requestTarget *t = new Adapter();t->Request();return 0;}

Object Adapter:

It can be seen that the client needs to call the request method, but adaptee does not have this method. To enable the client to use the adaptee class, a Wrapper class adapter is required. This packaging class encapsulates an adaptee instance to connect the client with adaptee. Since the adapter and adaptee are delegates, this determines that the adapter mode is object.

The role involved in this adapter mode includes:

Target role: This is the interface the customer expects. The target can be a specific or abstract class or interface.
Source (adaptee) role: the class to be adapted.
Adapter role: a adaptee object is encapsulated internally to convert the source interface to the target interface.

Simple implementation:

#include<iostream>using namespace std;// "ITarget"class Target{public:// Methodsvirtual void Request(){};};// "Adaptee"class Adaptee{public:// Methodsvoid SpecificRequest(){cout<<"Called SpecificRequest()"<<endl;}};// "Adapter"class Adapter : public Target{private:Adaptee *adaptee;public:Adapter(){adaptee = new Adaptee();}// Implements ITarget interfacevoid Request(){// Possibly do some data manipulation// and then call SpecificRequest  adaptee->SpecificRequest();}};int main(){// Create adapter and place a requestTarget *t = new Adapter();t->Request();return 0;}

Default adapter:

The default adapter mode is a special adapter mode, but this adapter is implemented by an abstract class and must implement all the methods specified in the target interface in the abstract class, but the implementation of many methods is "mediocre", that is, these methods are empty methods. The specific subclass must inherit this abstract class.

Simple implementation:

# Include <iostream> using namespace STD; Class target {public: Virtual void F1 () {}; virtual void F2 () {}; virtual void F3 (){};}; class defaultadapter: public target {public: void F1 () {} void F2 () {} void F3 () {}}; Class myinteresting: Public defaultadapter {public: void F3 () {cout <"oh, I'm interested in the F3 () method. I don't care about anything else! "<Endl ;}; int main () {// create adapter and place a requesttarget * t = new myinteresting (); t-> F3 (); Return 0 ;}


Implementation points:

1. the adapter mode is mainly used in scenarios where you want to reuse some existing classes, but the interfaces are inconsistent with the requirements of the reuse environment. It is useful in legacy code reuse and class library migration.

2. the adapter mode has two forms of implementation structure: Object Adapter and class Adapter. However, the class adapter adopts the "Multi-inheritance" implementation method, resulting in poor high coupling. Therefore, it is generally not recommended. The Object Adapter adopts the "Object combination" method, which is more compliant with the loose coupling spirit.

3. The implementation of the adapter mode can be very flexible, so you do not have to stick to the two structures defined in gof23. For example, the "existing object" in the adapter mode can be used as a new interface method parameter to achieve adaptation.

4. the adapter mode requires us to use the "interface-oriented programming" style as much as possible, so that we can easily adapt to it later.


Use Cases:

Use the adapter mode in the following situations:

1. The system needs to use an existing class, and such an interface does not meet the requirements of the system.

2. Create a reusable class for some classes that are not highly correlated with each other, including some classes that may be introduced in the future. These source classes do not necessarily have very complex interfaces.

3. (For object adapters) in the design, you need to change the interfaces of multiple existing sub-classes. If you use the class adapter mode, you need to create an adapter for each sub-class, which is not practical.


Lcl_data was originally created in csdn. Net [http://blog.csdn.net/lcl_data/article/details/8780140]


References:

1. http://www.cnblogs.com/zhenyulu/articles/39386.html

2. http://www.cnblogs.com/Terrylee/archive/2006/02/18/333000.html


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.