[Design mode] adapter Mode

Source: Internet
Author: User
Introduction

Yesterday, when charging the new MP3 player, it was found that the MP3 player only provides a USB interface charging method, and its charger cannot directly charge the USB interface, smart manufacturers installed a USB Interface Converter on the charger to solve the problem.

This USB interface adapter is the adapter that we will talk about today.

In software development, encoding techniques similar to the preceding method are called the adapter mode.

Definition and Structure

The design pattern book defines the adapter pattern as follows: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 incompatibility of interfaces to work together.

The example given in the introduction shows that the function described in this definition is consistent with the function of the actual adapter.

Maybe you still don't quite understand why you need to use the adapter mode.

Let's give an example to solve the problem more directly.

For example, in a drawing applet, you have implemented the drawing functions such as points, straight lines, and blocks. In addition, an abstract class is used to standardize the interfaces of these images to avoid the need to care about their differences when using the client program.

Now you want to plot the circle. At this time, you find that the circle has been drawn elsewhere in the system.

Fortunately, you can find that the existing methods in the system are different from the method name you specified in the abstract class!

What can I do?

To modify the method name of the circle, you must modify all the places where you use it. To modify the method name of your abstract class, you must also modify the implementation methods and existing references of all images.

Are there other methods? That is, the adapter mode.

We can see thatThe adapter mode is used for better reuse in interface-oriented programming.

  If interface-oriented programming is not used in your system and polymorphism is not used, I don't think the adapter mode will be used.

Composition of the adapter Mode
  1. Target role: defines the interface used by the client.
  2. Adaptee role: this role has an existing and used interface, and this interface needs to be adapted.
  3. Adapter role: the core of this adapter mode. It converts the existing interface of the adaptation role to the interface expected by the target role.

Put a simple class diagram, which is only a case of adapter mode implementation:

  

Category

In design patterns, the adapter patterns are dividedClass adapter ModeAndObject Adapter Mode.

The difference is that the adapter role is inherited or combined to adapt to the role.

Because Java does not support multi-inheritance, and inheritance may damage encapsulation, many books (including design patterns) advocate the use of combinations to replace inheritance.

Therefore, we will not introduce the class adapter mode here (in fact, it is rarely used ).

The above class diagram describes the Object Adapter mode. Adapter converts adaptee through combination.

Example

Next, let's look at the example of the drawing program above, first look at the class structure before adding the drawing circle:

  

The class structure after the circle is added:

  

We can see that the relationships between shape, circle, and textcircle correspond to those of target, apater, and apatee in standard adapter mode.

We only care about how the adapter mode is used in this drawing program.

Let's look at the implementation code of circle:

Class circle extends shape {// textcircle private textcircle TC; public circle () {Tc = new textcircle (); // initialization} void public display () {} TC. displayit (); // call the original textcircle method in the specified method }}
View code

 

This simple adapter implementation is complete.

In fact, the adapter role can not only complete the interface conversion process, but also improve and expand its functions. Of course, this is not within the scope of the adapter mode description.

The main difference between the two is that

  The proxy mode does not change the interface name, but also controls the functions of existing interfaces. The adapter mode emphasizes Interface Conversion.

Digress

In Java, there is an application called "Default Adaptation Mode", which is totally different from what we call the adapter mode.

The default adaptation mode provides a default implementation for an interface, so that the type can be extended from the default adaptation mode, avoiding the need to implement some interfaces that you do not care about when expanding from the original interface.

The xxxadapter in Java. AWT. event is a good example. If you are interested, please take a look.

 

 

@ Cheng Peng Zhiyuan

(Blogs: lcw.cnblogs.com)

(Email:[Email protected])

(Qq:552158509)

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.