Thanks to "Android Source Code Design Pattern Analysis and Practical Practice" He Honghui cared for the people
The adapter mode is extremely useful in our development. It can be judged from the Adapters that can be seen everywhere in the code. From the earliest ListView, GridView, and the latest RecyclerView, we all need to use Adapters, and the optimization problems and places with high probability of errors we encounter in development are basically derived from Adapters.
Adapters are to fuse two incompatible fire dragons together, and use a transformation to enable them to collaborate. For example, it is often encountered that you want to interact between two unrelated types. The first solution is to modify the interfaces of each class. However, if there is no source code or we are unwilling to modify their respective interfaces for an application, in this case, we often use an Adapter, which will compatible these two interfaces and meet the needs without modifying the original code.
Suppose there is already a software system, and you want it to be used with a new manufacturer's class library, but the interface designed by this new manufacturer is not used for the old manufacturer's interface:
What should you do if you don’t want to change the existing code and solve this problem (and you can’t change the manufacturer’s code)? You can write a class (adapter) that converts the new vendor interface into the interface you expect. This adapter works like an intermediary, which converts the requests issued by the customer into requests that the vendor class can understand.
Adapter modes can be divided into two types:
Object adapter: full of good OO design principles. Using object combinations, it can be applied to the adapter as an interface and all its subclasses. The adapter method cannot be rewrite because there is no inheritance relationship, but it can also "reimplement" the method in the adapter. The client and the adapter are completely irrelevant, and only the adapter has references to the adapter.
Class adapter: Use inheritance to achieve the work of adapter. Only the adapter is an interface, and cannot use its subclass interface. When the class adapter is established, it is statically associated with the adapter. The adapter serves as the base class of the adapter, so the adapter can rewrite the methods in the adapter. The client code is visible to the code declared in the adapter. The client code is visible to the code declared in the adapter.
definition:
The adapter pattern transforms the interface of one class into another interface that the client expects, so that two classes that were originally unable to work together due to interface mismatch can work together.
Use scenarios:
1. The system needs to use existing classes, and such interfaces do not meet the needs of the system, that is, the interfaces are incompatible.
2. Want to create a class that can be reused to work with some classes that are not very related to each other, including some classes that may be introduced in the future.
3. A unified output interface is required, and the type of the input end is unpredictable.
UML class diagram:
First look at the following adapters:
Class adapters implement interface conversion by implementing the Target interface and inheriting the Adaptee class. For example, the target interface requires operation2, but the Adaptee object only has one operation3, so there is incompatible situations. At this time, an operation2 function is implemented through Adapter to convert Adaptee's operation3 into operation2 required by Target, so as to achieve compatibility.
Target: The target role, that is, the interface you are looking for. Note: Since the class adapter pattern is discussed here, the target cannot be a class.
Adaptee: The adapted interface is now required.
Adapter: The adapter role is also the core of this mode. The adapter converts the source interface into the target interface. This role cannot be an interface, but must be a specific class.
Class adapter mode example:
Take the mainland voltage is 220v and the mobile phone voltage is 5v as an example
/** * Voltage class Target target* @author Administrator * */public interface Voltage { public int getVoltage();}public class ChinaVoltage implements Voltage{ @Override public int getVoltage() { // Continental voltage is 220 return 220; }}<pre name="code">/** * Mobile phone class, Adaptee adapter class* @author Administrator * */public class PhoneVoltage { /** * Mobile phone voltage is 5v * @return */ public int getPhoneVoltage() { return 5; }} <pre name="code">/** * Charger Adapter Administrator * */public class Charger extends PhoneVoltage implements Voltage { @Override public int getVoltage() { return getPhoneVoltage(); }} public class Client { public static void main(String[] args) { ChinaVoltage vol = new ChinaVoltage(); System.out.println("The mainland voltage is: " + vol.getVoltage()); //The voltage when connecting the charger to the mobile phone Chargerr character = new Chargerr(); System.out.println("Voltage converted through the charger: " + character.getVoltage()); }} Running results:
The voltage in mainland China is: 220
Voltage converted through the charger: 5
Let’s take a look at the object adapter class diagram:
The following is to modify the Chargerr class using the object adapter
<pre name="code">/** * Charger Adapter Administrator * */public class Chargerr implements Voltage{ private PhoneVoltage phoneV; public Chargerr(PhoneVoltage phoneV) { this.phoneV = phoneV; } @Override public int getVoltage() { return phoneV.getPhoneVoltage(); }}The object adapter implementation method directly passes the object to be adapted into Adapter, and uses a combined form to achieve the effect of interface compatibility. This is more flexible than the class adapter method. Another advantage is that the methods in the adapted object will not be exposed. Since the class adapter inherits the adapted object, the functions of the adapted object class are also contained in the Adapter class, which makes the Adapter class have some strange interfaces, which is more cost-effective for users. Therefore, the object adapter mode is more flexible and practical.
public class Client { public static void main(String[] args) {// ChinaVoltage vol = new ChinaVoltage();// System.out.println("The mainland voltage is: " + vol.getVoltage());// // The voltage when connecting the phone to the charger// Chargerr character = new Chargerr();// System.out.println("Voltage converted through the charger: " + character.getVoltage()); // Adapted PhoneVoltage phoneV = new PhoneVoltage(); Chargerr charger = new Chargerr(phoneV); System.out.println("Voltage converted through the charger:" + charger.getVoltage()); } //Operation result: //Voltage converted through the charger: 5} Summarize:
The classic implementation of the Adapter model is to integrate originally incompatible interfaces so that they can cooperate well. However, in actual development, the Adapter model also has some flexible implementations. For example, the isolation changes in ListView make the entire UI architecture more flexible and can embrace changes. The Adapter model is widely used in development.
advantage:
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 better reused through adapter mode.
Better scalability: When implementing the adapter function, you can call the functions you developed by yourself to naturally expand the functions of the system.
shortcoming:
Too much use of adapters will make the system very messy and difficult to grasp. For example, the Chang A interface that is called is actually adapted to the implementation of the B interface. If there are too many cases of this in a system, it is tantamount to a disaster. Therefore, if it is not necessary, the adapter can be used instead of refactoring the system directly.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.