20th Chapter, Adapter mode
The adapter pattern is one of the structural design patterns, and it is used very well in our development, for example, the ListView, GridView, and Recyclerview need to use adapter.
1. Definition
The adapter mode transforms the interface of a class into a interface that the client expects, so that two classes that would otherwise not work together can work together.
2. Usage Scenarios
(1) The system needs to use the existing classes. However, such interfaces do not meet the needs of the system. That is, the interface is incompatible.
(2) Want to create a class that can be reused. For classes that are not too much related to each other, including some classes that might be introduced in the future.
(3) requires a unified output interface, and the input terminal type is unpredictable.
3.UML class Diagram
The adapter pattern is divided into two types, the class adapter mode and the object adapter mode. The following is a uml diagram of a class adapter.
(1) Target
: Target role. That is, the interface you expect to get. Note: Because the class adapter pattern is discussed here. Therefore, the target cannot be a class.
(2) Adaptee
: Interfaces that need to be adapted today.
(3) Adapter
: The adapter role, which is the core of this mode. The adapter converts the source interface into the target interface. Obviously, this role cannot be an interface, but must be a detailed class.
4. Simple implementation
In the case of a laptop power adapter, the power adapter converts the 220V voltage to 5V.
Then the 5V voltage is the target interface, 220V voltage is the Adaptee class, the conversion is adapter.
(1) Implemented in class adapter mode
//Target角色publicinterface FiveVolt { publicintgetVolt5();}
//adapter role Public class Volt220 {public int getvolt220 () {return 220 ; }}
public class voltadapter extends volt220 implements fivevolt { @Override public int getvolt5 () {return
5 ; }}
publicclass Test { publicstaticvoidmain(String[] args) { new VoltAdapter(); System.out.println("输出电压:" + adapter.getVolt5()); }}
Results:
输出电压:5
(2) object Adapter mode implementation
The object adapter pattern differs from the class adapter mode. Instead of using an inheritance relationship to connect to the adapter class, the object adapter pattern uses a proxy relationship to connect to the adapter class.
So Fivevolt and Volt220 are not changed. Voltadapter changes such as the following:
public class voltadapter1 implements fivevolt { Volt220 mVolt220; public voltadapter1 (Volt220 adaptee) {this . mVolt220 = adaptee; } public int getVolt220 () {return mvolt220.getvolt220 (); } @Override public int getvolt5 () {return
5 ; }}
Test changes to:
publicclass Test { publicstaticvoidmain(String[] args) { new VoltAdapter(new Volt220()); System.out.println("输出电压:" + adapter.getVolt5()); }}
The implementation of this way directly to the object to be adapted to the adapter, using a combination of the form of the interface to achieve compatibility effect. This is more flexible than the class adapter approach, and the method of adapting the object at the same time is not exposed. Therefore, the object adapter mode is more flexible and useful.
Adapter of Adapter mode 1.ListView in 5.Android source code
The adapter of the ListView here is the object adapter pattern used, the target is the view,adapter role is to output the item view as the role of view abstraction, Adaptee is the item view that needs to be processed.
6. Summary 1. Strengths
(1) Better reusability: The system needs to use the existing classes. This type of interface does not meet the needs of the system. The adapter mode allows for better reuse of these features.
(2) Better extensibility: When implementing the adapter function, it is possible to invoke the features that you have developed to naturally extend the functionality of the system.
2. Disadvantages
Excessive use of the adapter, will make the system very messy, not easy to grasp the overall. For example, clearly see the call is a interface, in fact, the interior is adapted to the implementation of the B interface, a system assumes that too many occurrences of such a situation, is tantamount to a disaster. It is therefore not very necessary to assume that the system can be refactored without the use of an adapter.
7. References
The adapter mode of Java and mode
"Android source code design mode analysis and actual combat" reading notes (20)