In-depth exterior adapter mode

xiaoxiao2021-03-06  40

First, the primer

When yesterday, I found this MP3 player only provided the USB interface to charge, and the charger it provided was not directly charged, and smart vendors were installed for the charger. The USB interface converter solves the problem.

This USB interface adapter is the adapter we have to talk about today. In software development, the encoding technique similar to the above method is called the adapter mode.

Second, definition and structure

The "Design Mode" book is this definition to the adapter mode: converts a class interface into another interface that customers want. Adapter mode makes it possible to work with those classes that are not compatible with interfaces. The example given in the primer can be seen that the functionality of this definition and the function of the adapter in the reality are consistent.

Maybe you still don't quite understand why you want to use the adapter mode. Let's give an example may be more directly to eliminate your doubts.

For example, in a picture of a drawing, you have implemented the function of drawing points, straight lines, squares. And in order to let the customer program do not need to care about them when using, an abstract class is used to specify the interfaces of these graphics. Now you have to achieve a circle drawing, then you find that there is already a reach of a circle in other places. Fortunately, you find that the methods existing in the system are different from the method specified in the abstract class! This is how to do? Modify the method name of the painting circulation, you must modify all places that use it; modify your abstract class method name, but also to modify all graphics implementations and existing references. Is there any other way? That is the adapter mode.

It can be seen that the use of the adapter mode is to be better reuse in the interface programming. If you do not use the interface programming in your system, it is not used to use the polymorphism, I think it will never use the adapter mode.

Let's take a look at the composition of the adapter mode.

1) Target Role: Define the interface used by the Client.

2) Adaptee role: This role has an interface that exists and uses, and this interface is usually suitable.

3) Adapter Role: The core of this adapter mode. It converts an interface that has been adapted to an existing interface into a target role desired interface.

Put a simple class diagram, which is just a situation in the adapter mode:

Third, classification

Divide adapter mode into class adapter mode and object adapter mode in the "Design Mode" book. The difference is that the adapter role is done by the adapted role to be completed by inheritance or by a combination. Since multiple inheritance is not supported in Java, it is advocated to use a combination in many books (including "design model") in Java (including "design model"). So we will no longer introduce class adapter mode (which is also very small).

The object adapter mode is described in the class diagram of the previous section. Adapter's conversion of Adaptee is done through a combination (if you don't understand the meaning of basic elements in the class map, please read my "UML class diagram").

Fourth, an example

Then, the example of the drawing program mentioned above, let's take a look at the class structure before adding the demand of the plot.

The class structure after the circle is drawn:

It can be seen that the relationship between the three of Shape, Circle and TextCircle is the relationship between Target, Apater, Apatee, in the standard adapter mode. We only care how to use the adapter mode in this drawing program. Take a look at the Circle's implementation code:

Class Circle Extends Shape

{

/ / Here quoted TextCircle

PRIVATE TEXTCIRCLE TC;

Public circle ()

{

Tc = new textcircle (); // Initialization

}

void public display ()

{

Tc.displayit (); // In the specified method, call TextCircle's original method}

}

Such a simple adapter is achieved.

In fact, the process of interface conversion can be completed in the adapter role, but also improve and expand its function, of course, this is not within the scope of the adapter mode.

As I introduced the agent model, the main difference between the two is the case where the agent mode is not changed, and it is a control for the existing interface function; and the adapter mode emphasizes the interface conversion.

V.

There is a application called "Default Adapter Mode" in Java, which is completely two of our adapter mode. The default adapter mode provides default implementation for an interface, so that the type can be extended from the default adaptation mode to avoid some interfaces that you don't care when expanding from the original interface. XXXADAPTER in java.awt.event is its good example, interested in see.

Six, summary

Simple introduction is relatively simple but very practical adapter mode. Please refer to if there is a hurt.

转载请注明原文地址:https://www.9cbs.com/read-62543.html

New Post(0)