Design mode adapter mode (Adapter)

Source: Internet
Author: User
Design mode adapter mode (Adapter)

I want to say the adapter mode, it should be more appropriate to mention the "voltage", "headset", "Charger" and "USB" examples. Just talk about your personal experience, my original mobile phone is NOKIA5730. Later did not know there is no country, but do not recommend you buy this phone, not applicable Kazakhstan. When I bought only Hong Kong goods, but you know Ah, Hong Kong's charger and our mainland charger specifications are not the same, then if I charge at home, the original machine with the charger is not used, so to spend 25RMB to buy a bull adapter, so that can be charged.

1. Initial knowledge of adapter mode

I think as an IT person, the adapter should not be unfamiliar, but in theory may not be described in place. In fact, a lot of system development, to use the third-party software, so that the interface of the third-party software and the software we develop the interface is often inconsistent, then it will be "fit". Look at the Gof of the adapter mode:

Adapter mode: The interface of one class is converted into another interface that the customer wants, and the adapter mode makes it possible for those classes that cannot work together because of incompatible interfaces ...

In general, the adapter contains several of the following roles:

Target Abstract Role: define the interface that the customer expects to use, we use the handset as the client, the charger that the client needs is the Continental standard.

source Role (Adaptee): the interface that needs to be adapted, here refers to the port charger that comes with my phone when I buy it.

Adapter Role (Adapter): The device used to convert the source interface into a target interface that meets the requirements, and here refers to the adapter of the bull I bought myself.

client: This is my port version of the phone.

now look at the structure diagram of the adapter mode:

              

Note: Here we only say consistent adapter mode: Object Adapter mode, not talk class adapter mode.

2. Examples of implementing adapter patterns

Now, let's see how the adapter pattern is implemented, and how to "use the port version of the charger in the mainland." ”

1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Targetcharger
 6 {
 7 public:
 8     virtual void chargerequest ()
 9     {
Ten     } One
};
Class Adapteecharger
:     specificrequest () ()         cout<< "Charging with port charger ... "<<endl;
}    
;
Class Adaptercharger:public Targetcharger
{
private:     adapteecharger adaptee;
Public:     virtual void chargerequest () (
cout<<)         : "The use of the adaptor, the bull charger,";         Adaptee. Specificrequest ();     }
; 
int main ()     Adaptercharger Adapter;     Targetcharger *target = &Adapter;
Panax Notoginseng     target->chargerequest ();     return 0;
39}

Observe the code and run the results, knowing that the strategy pattern is actually implemented. The purpose of using the Hong Kong version of the charger in the mainland has indeed been completed. So let's think about the benefits of using the adapter model. Suppose, now I bought a mobile phone in Europe, then the mobile phone can not recharge the mainland, how to do it. At this point we only need to write a class to inherit the Targetcharget class to complete the adaptation of the European version of the charger, so that we can easily use the European phone. What do you think. Using the adapter mode also provides a unified interface.
3. Occasions and benefits of implementing adapter mode

First, it must be emphasized that the adapter pattern is suitable for use with third-party software, and that the interface provided by third-party software is inconsistent with the interface we develop, and that the system we are developing is not easy to change the interface, so it is better to use the adapter at this time. In fact, the adapter mode is more like a make-up mode, when the interface is inconsistent, and the system development has entered a very difficult to change, this time can use the adapter mode, but if in the early development of the interface is inconsistent, try not to use the adapter mode, but finally the interface design consistency is better. This means that most of the adapter patterns are used in software development later in a design pattern.

for specific applications:

1. The system under development wants to use an already existing class, and the class is important, but the interfaces and systems provided by the class are inconsistent.

2. Third-party software is used and the interfaces and systems of the software provided by the third party are inconsistent.

3. Two existing classes complete the same functionality, but the interfaces are different. The adapter mode can be used to provide a consistent interface.

4. Reuse of the old system.

benefits of using adapter mode:

1. Make full use of existing resources, realize the reuse of software, save development cost and time.

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.