Adapter pattern of Java design pattern notes

Adapter mode:

The adapter pattern transforms the interface of a class into another interface expected by the client, so that two classes that could not work together because of interface mismatch can work together.

Scenes in life:

1. Notebook power adapter can convert 220V into a voltage suitable for notebook use.

2. To plug the USB interface of the laptop into the keyboard of the PS / 2 interface of the desktop, a USB and PS / 2 interface adapter is required. At this time, the USB and PS / 2 interface adapter acts as an adapter.

General class diagram:

In the general class diagram above, The cient class finally faces the target interface (or abstract class), which can only use subclasses that meet this target standard; while the Adaptee class is the adapted object (also known as the source role) because it contains specific (special) operations, functions, etc., so we want to use it in our own system and convert it into a class that meets our standards, so that the client class can choose to use the concretetarget class or the Adaptee class with special functions under the condition of transparency.

Roles in adapter mode:

Target interface: the interface expected by the customer. The target can be a concrete or abstract class or an interface. Adaptee: the interface or adaptation class that needs to be adapted. Adapter: the adapter class is the core of this pattern. The adapter converts the source interface into the target interface by wrapping an object that needs to be adapted. Obviously, this role cannot be an interface, but must be a concrete class.

Structure of adapter mode:

The adapter pattern has two different forms: the adapter pattern of class and the adapter pattern of object.

Class adapter pattern converts the API of the adapted class into the API of the target class.

The adapter mode of the object is the same as that of the class. The adapter mode of the object converts the API of the adapted class into the API of the target class. Different from the adapter mode of the class, the adapter mode of the object is not connected to the Adaptee class using inheritance relationship, but connected to the Adaptee class using delegation relationship.

Adapter pattern for class

1. Create an adapted class:

2. Create a target interface that can handle some special requests

3. Create an adapter (class adapter mode)

4. Create a client

The adapter implemented above is called class adapter, because the adapter class inherits Adaptee (the adapted class) and implements the target interface (Java does not support multiple inheritance, so it is implemented in this way). In the client class, we can select and create any subclass that meets the requirements as needed to realize specific functions.

Adapter mode for object

1. Create an adapted class:

2. Create a target interface that can handle some special requests

3. Create an adapter (object adapter mode, which uses a combination mode to integrate with the adapted object)

4. Create a client

We only need to modify the internal structure of the adapter class, that is, the adapter itself must first have an object of the adapted class, and then delegate the specific special functions to this object to implement. Using the object adapter mode, the adapter class (adapter class) can adapt multiple different adapted classes according to the incoming Adaptee object. Of course, at this time, we can extract an interface or abstract class for multiple adapted classes. In this way, it seems that the object adapter mode is more flexible.

Trade off between class adapter and object adapter:

It is recommended to use the implementation of object adapter as much as possible, using more composition / aggregation and less inheritance. Of course, specific problems are analyzed, and the implementation method is selected according to the needs. The most suitable is the best.

Advantages of adapter mode:

Better reusability:

The system needs to use existing classes, and such interfaces do not meet the needs of the system. Then these functions can be reused better through the adapter mode.   

Better scalability:

When implementing the adapter function, you can call the functions developed by yourself, so as to naturally expand the functions of the system. Disadvantages of adapter mode

Excessive use of adapters will make the system very messy and difficult to grasp as a whole. For example, it is obvious that the a interface is called, but in fact, it is internally adapted to the implementation of B interface. If this happens too often in a system, it is tantamount to a disaster. Therefore, if it is not necessary, you can refactor the system directly instead of using adapters.

Scenario of adapter mode in operation:

1. The interfaces of existing classes do not meet our requirements; 2. Create a reusable class so that it can work with other unrelated classes or unforeseen classes (i.e. those whose interfaces may not be compatible); 3. Use some existing subclasses without subclassing each to match their interfaces.

Adapter mode is often used for old system transformation and upgrading. If our system does not need maintenance after development, many patterns are unnecessary. Unfortunately, the cost of maintaining a system is often several times that of developing a system.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>