Adapter mode in Design Mode)

Source: Internet
Author: User

I 'd like to talk about the adapter mode. It should be appropriate to mention the "voltage", "headset", "charger" and "USB" examples. Let's talk about my personal experience. My old mobile phone number is nokia5730. I don't know if there is any country bank in the future, but I don't recommend you buy this mobile phone. It's not applicable. I only purchased Hong Kong products, but as you know, the charger specifications in Hong Kong are different from those in mainland China. If I charge a charger in China, the charger with the original model won't work at all, so we need to spend 25 RMB to buy a bull's adapter so that we can charge it !!

1. First recognized adapter Mode

I think as an IT person, there should be no stranger to the adapter, but theoretically it may not be well described. In fact, third-party software is used for the development of many systems. In this case, the interfaces of third-party software and the interfaces of the software we develop are often inconsistent, then it is necessary to "Adapt. Let's take a look at the gof Statement on the adapter mode:

Adapter mode: converts an interface of a class to another interface that the customer wants. The adapter mode allows the classes that cannot work together due to interface incompatibility !!!

Generally, the adapter contains the following roles:

Target abstract role (target ):Define the interface that the customer expects to use. We use the mobile phone as the client, and the charger used by the client is a Chinese standard.

Source role (adaptee ):The interface to be adapted refers to the port charger that comes with my mobile phone when I buy it.

Adapter role ):It is used to convert the source interface into a device that meets the requirements for the target interface. Here it refers to the adapter of the bull I bought myself.

Client ):This is my Hong Kong mobile phone.

Now let's take a look at the structure of the adapter mode.:

 

Note: The Object Adapter mode is the same as the class adapter mode.

2. Examples of implementing the adapter Mode

Now, let's see how the adapter mode is implemented. How can we "Use the Hong Kong version of the charger in mainland China ??"

View code

 1 # Include <iostream> 2   3   Using   Namespace  STD;  4   5   Class  Targetcharger  6   {  7  Public  :  8       Virtual   Void  Chargerequest ()  9   {  10   }  11   };  12   Class  Adapteecharger  13   { 14   Public  :  15       Void  Specificrequest ()  16   {  17 Cout < "  Use the Hong Kong charger for charging !!!  " < Endl;  18   }  19  };  20   Class Adaptercharger: Public  Targetcharger  21   {  22   Private  :  23   Adapteecharger adaptee;  24   Public  :  25       Virtual  Void  Chargerequest ()  26   {  27 Cout < "  Used adapter: Bull charger,  "  ;  28   Adaptee. specificrequest ();  29   }  30   };  31      32   33   Int  Main ()  34   {  35   Adaptercharger adapter;  36 Targetcharger * target = & Adapter;  37 Target-> Chargerequest ();  38       Return   0  ; 39 }

ObservationCodeAnd the running results, we know that the policy mode is indeed implemented !! It has indeed completed the use of the Hong Kong version charger in mainland China. So what are the benefits of using the adapter mode? Assume that I have bought another mobile phone in Europe, and the mobile phone cannot be recharged in mainland China. What should I do? At this time, we only need to write a class to inherit the targetcharget class to adapt to the european charger, so that we can easily use the European mobile phone. How is it? The adapter mode also provides unified interfaces.
3. Compatibility and benefits of the adapter Mode

First, it must be emphasized that the adapter mode is applicable to the use of third-party software, and the interfaces provided by the third-party software are inconsistent with the system interfaces we developed, at the same time, it is not easy for the system we are developing to change the interface. It is better to use the adapter at this time. In fact, the adapter mode is more like a compensatory mode. When the interfaces are inconsistent and the system development is difficult to change, you can use the adapter mode at this time, however, if the interface inconsistency is found in the early stages of development, try not to use the adapter mode. Instead, it is better to design the interface in the end. That is to say, most of the adapter modes are used later in software development.

Applicable scenarios:

1. the system being developed wants to use an existing class, and this class is very important, but the interfaces provided by this class are inconsistent with those provided by the system.

2. Third-party software is used, and the interfaces of the software provided by the third party are inconsistent with those of the system.

3. The functions of the two existing classes are the same, but the interfaces are different. You can use the adapter mode to provide consistent interfaces.

4. Reuse of old systems.

Benefits of using the adapter mode:

1. Make full use of existing resources to reuse software and save development costs and time.

 

Let's take a look at some of the lessons learned.

 

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.