C + + Design mode adapter Mode _c language

Source: Internet
Author: User

The adapters in life

Buy a laptop, buy a mobile phone, there is a power adapter, the power adapter is called external power supply, is a small portable electronic equipment and electrical power supply voltage conversion equipment, commonly used in mobile phones, notebook computers. Its role is to convert the home's 220V high voltage to these electronic products can work 5v~20v low voltage, so that they can work properly. In other words, without this power adapter, our phones and computers will not be able to charge.

My colleague went to Japan for a business trip, so I took my notebook with me because of my work. On the night of the tragedy, the notebook can not be used. Because Japan's residents are 110V of electricity, and China is 220V, the colleague's notebook is 220V-powered. The next day, the colleague went to buy a voltage adapter. If there is no voltage adapter, this trip is expected to be a tragedy.

What is adapter mode?

Having said so many examples of adapters in your life, what is the adapter in the software design and development process?

In Gof's design pattern: The basics of reusable object-oriented software: Converting the interface of a class into another interface that customers want. Adapter mode makes it possible for those classes that cannot work together because of incompatible interfaces. Like Japan now only provide 110V voltage, and my computer needs 220V voltage, then what to do? Adapters are doing the work, building a bridge between incompatible things, and making the two compatible together.

Why should I use adapter mode?

In software development, sometimes the system's data and behavior are correct, but the interface does not match, we should consider using adapter mode, the purpose is to control the scope of an existing object and an interface match. For example: When developing a module, there is an effort to achieve a functional point, but the previous project has a module to achieve the same function point, but now the interface of the module and the previous module of the interface is inconsistent. Now, what do you do as a project manager? Of course, in the middle of a layer of wrapper, that is, the use of adapter mode, the previously implemented functional points fit into the new project. Why, then? The main advantages of using the adapter pattern are the following:

1. Reduce the difficulty to achieve a function point, you can pack the existing classes, you can use;

2. Improve the quality of the project, the existing classes are generally tested, after using the adapter mode, do not need to carry out a comprehensive coverage of the old class test;

3. Overall, increased efficiency and reduced costs.

When do I use adapter mode?

1. Each design pattern has its most applicable occasion. Adapter mode is best applied in the following situations:

2. Using an existing class, consider using adapter mode if its interface is inconsistent with your actual requirements;
Instead of using adapter mode when both the caller and the feature provider are not easily modified, use it instead of a different one.

UML Class Diagram

The above illustration is the first implementation form of the adapter pattern, and the adapter adapter inherits from the target and Adaptee classes, and the adapter class needs to rewrite the request function of the target class to do the appropriate processing in the request. Invokes the sepcificrequest of the Adaptee class. Ultimately, Target actually calls the Adaptee specificrequest to complete the request, which is called the class adapter.

The image above is the second implementation of the adapter, and the adapter adapter class inherits from the target class, while in the adapter class there is a adaptee type of member variable; When the adapter class overrides the request function, in the request, Call the Adaptee specificrequest function using the Adaptee type's member variable, which is called the object adapter.

Comparison of class adapters and object adapters

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:

1. Since the adapter directly inherits from the Adaptee class, the Adaptee class method can be redefined in the adapter class;

2. If an abstract method is added to the Adaptee, the adapter should also be changed accordingly, thus resulting in high coupling;

3. Using a class adapter is not possible if Adaptee has other subclasses, and in adapter you want to invoke methods that adaptee other subclasses.

Object adapters have the following characteristics:

1. Sometimes, you will find that it is not easy to construct an object of adaptee type;

2. When new abstract methods are added to the Adaptee, the adapter class does not need to make any adjustments, and it can perform the action correctly.

3. A method of calling Adaptee class subclasses in a adapter class can be used in a polypeptide way.

Because of the low coupling of object adapters, object adapters are recommended in many books. In our actual project, too, we can use the method of object composition, do not use the way of multiple inheritance.

Code implementation

Implementation code for class adapters

Copy Code code as follows:

/*
* * Filename:adapterpatterndemo
* * author:jelly Young
* * DATE:2013/11/27
* * Description:more information, http://www.jellythink.com
*/

#include <iostream>
using namespace Std;

Targets
Class Target
{
Public
virtual void Request ()
{
cout<< "Target::request" <<endl;
}
};

Adaptee
Class Adaptee
{
Public
void Specificrequest ()
{
cout<< "Adaptee::specificrequest" <<endl;
}
};

Adapter
Class Adapter:public Target, Adaptee
{
Public
void Request ()
{
Adaptee::specificrequest ();
}
};

Client
int main (int argc, char *argv[])
{
Target *targetobj = new Adapter ();
Targetobj->request ();

Delete targetobj;
Targetobj = NULL;

return 0;
}

Code implementation for object adapters

Copy Code code as follows:

/*
* * Filename:adapterpatterndemo
* * author:jelly Young
* * DATE:2013/11/27
* * Description:more information, http://www.jellythink.com
*/

#include <iostream>
using namespace Std;

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_adaptee;
M_adaptee = NULL;
}
}

void Request ()
{
M_adaptee->specificrequest ();
}

Private
Adaptee *m_adaptee;
};

int main (int argc, char *argv[])
{
Target *targetobj = new Adapter ();
Targetobj->request ();

Delete targetobj;
Targetobj = NULL;

return 0;
}

Summarize

Adapter mode is easy to understand and realize, in the future project, a lot of practice, will learn the theoretical knowledge to apply to the actual project, write beautiful code.

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.