[C + + Design mode] Adapter Adapter mode

Source: Internet
Author: User

In STL, a stack encapsulates a vector or a double-ended queue, and the interface that provides the stack operation is the typical adapter pattern.

Converting the interface of one class into another interface that the customer wants is the adapter mode.

Using adapter mode has the following advantages:

Reduce the difficulty to achieve a functional point, you can wrap the existing class, you can use it;
Improve the quality of the project, the existing classes are generally tested, using the adapter mode, do not need to complete the old class coverage testing;
Overall, it improves efficiency and lowers costs.

Depending on the combination and inheritance of classes, the adapter pattern is divided into Object adapter mode and class adapter mode.



Now that you have a class adapter and an object adapter, how do you choose between the two in practice?

Class adapters have the following characteristics:

Because adapter directly inherits from the Adaptee class, the method of Adaptee class can be redefined in the adapter class;
If an abstract method is added to the adaptee, then the adapter should be changed accordingly, thus resulting in high coupling;
It is not possible to use a class adapter if there are other subclasses in the adaptee and if you want to invoke a method of Adaptee other subclasses in adapter.
The object adapter has the following characteristics:

Sometimes, you will find that it is not easy to construct an object of the adaptee type;
When adding a new abstract method to the Adaptee, the adapter class does not need to make any adjustments and can perform the action correctly;
You can call a method of the Adaptee class subclass in a adapter class using polymorphic methods.
Object adapters are recommended in many books because of the low degree of coupling of the object adapters. The same is true in our actual project, where you can use the combination of objects without using a multi-inheritance approach.

Implementation code for the class adapter:

Targetsclass target{public:virtual void Request () {cout<< "Target::request" <<endl;}};/ /Adapteeclass adaptee{public:void specificrequest () {cout<< "Adaptee::specificrequest" <<endl;}};/ /Adapterclass adapter:public Target, adaptee{public:void Request () {adaptee::specificrequest ();}};/ /clientint Main (int argc, char *argv[]) {Target *targetobj = new Adapter (); Targetobj->request ();d elete targetobj; Targetobj = Null;return 0;}

Object Adapter Mode code:

Class Target{public:target () {}virtual ~target () {}virtual void Request () {cout<< "Target::request" <<endl;}}; Class Adaptee{public:void Specificrequest () {cout<< "Adaptee::specificrequest" <<endl;}}; Class Adapter:public Target{public:adapter (): M_adaptee (new Adaptee) {}~adapter () {if (m_adaptee! = NULL) {Delete m_adapt Ee;m_adaptee = NULL;}} void Request () {m_adaptee->specificrequest ();} Private:adaptee *m_adaptee;}; int main (int argc, char *argv[]) {Target *targetobj = new Adapter (); Targetobj->request ();d elete targetobj;targetobj = N Ull;return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[C + + Design mode] Adapter 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.