1. Introduction to Adapter Mode
In our daily life, we will often come into contact with the adapter mode, such as our charger, different phone interface types may not be the same, this time as long as a switch interface , you can solve our problem. This switch interface is our adapter.
Defined
The adapter mode transforms the interface of one class into another interface that the client expects, so that two classes that cannot be together because of an interface mismatch can work together.
2. Usage scenarios for Adapter mode
- The system needs to use the existing classes, and the interfaces of this class do not meet the needs of the system, that is, the interface is incompatible.
- You want to create a reusable class that works with some classes that are not too much related to each other, including some that might be introduced in the future.
- A unified output interface is required, and the type of the input terminal is unpredictable.
3. UML class Diagram Class adapter mode for Adapter mode UML:
Object Adapter Mode UML:
4. Simple instance class adapter mode for Adapter mode
publicinterface Target { /** * 源Adaptee中有的方法 */ publicvoidoperation1(); publicvoidoperation2();}
There are two methods in the target interface, Operation1 () is a method in the source Adaptee, and Operation2 () is not a method.
publicclass Adaptee { publicvoidoperation1() { System.out.println("operation1"); }}
There is only one method in the Adaptee role that is the same as the name in the interface Operation1 ().
publicclass Adapter extends Adaptee implements Target { @Override publicvoidoperation2() { //相关代码 System.out.println("operation2"); }}
In the adapter role, the Adaptee class was inherited, and the target interface was implemented, so that there were two methods in Adaptee.
publicclass Client { publicstaticvoidmain(String[] args) { new Adapter(); adapter.operation1(); adapter.operation2();}
Object Adapter Mode
The difference between object adaptation mode and class adaptation mode is that in the adapter class, the adapter class for the object adaptation pattern is as follows:
publicclass Adapter { private Adaptee adaptee; publicAdapter(Adaptee adaptee) { this.adaptee = adaptee; } publicvoidoperation1(){ this.adaptee.operation1(); } publicvoidoperation2(){ }}
In the construction method of the adapter, pass in a Adaptee object reference, adapter class has the interface two methods, Operation1 () directly calls Adaptee () method; Operation1 () Implement the specific logic.
Choose that method?
In the use of adapter, try to use the implementation of the object adapter, multi-use composition or aggregation, less inheritance. Because a class can inherit only once, it has limitations.
5. Adapter mode in Android source code
We have used the ListView in development, so that the listview fills up with the adapter,adapter function of populating the different item into different view. The adaptee here is the item view that needs to be processed, and theTarget role is view.
6. Summary
Advantages:
Better reusability. The system needs to use the existing classes, and the interfaces of this class do not meet the needs of the system. The adapter mode allows for better reuse of these features.
Better extensibility. When implementing the adapter functionality, you can invoke the features you have developed to naturally extend the functionality of the system.
Disadvantages:
- Excessive use of the adapter, will make the system very messy, not easy to grasp the overall. For example, call a interface, but call the implementation of B.
19. Adapter design mode