Adapter mode in Design Mode
Adapter mode. This design mode is easy to understand, because it is too common in life. For example, the power adapter and socket provide a voltage of V, while my computer uses a voltage of V, and the arm uses a voltage of 5 V. If the target is very small, the power supply can be customized for you, but when there is a lot of voltage demand, there will be no way to provide a variety of needs, then the power adapter will come in handy, with one adapter, you can easily obtain the required voltage. There is also a translation. For example, a website provides a general English source, and users only understand Chinese, a translator must act as an adapter for adaptation.
The adapter mode is everywhere in our learning process. For example, the C ++ stack/queue is the so-called adapter container, and many users have such requirements, A dual-end queue provides this function, but the interface is not the interface you want, you can use the stack adapter to perform the corresponding conversion. In android, adapters are more common. For example, listview requires various types of data, but the data itself does not provide corresponding interfaces. Therefore, various adapters are required to encapsulate the data, then it is used by listview.
The application is mentioned above, and the role in the adapter is described below (take the power adapter as an example:
(1) Target: arm, which requires 5 V voltage for arm operation;
(2) Class Adaptee to be adapted (source class): it can provide a voltage of V and cannot meet the needs of the target;
(3) Adapter: A power Adapter that converts a voltage of V to a voltage of 5 V.
Adapters are divided into the class adapter mode and the Object Adapter mode. You can associate the interfaces of a system with the classes of another system that is originally incompatible, so that the two mines can work together, the conversion of interfaces is emphasized.
I'm too lazy to draw my own UML...
----------------------------------------------------------------------------------
The following describes the implementation source code, JAVA:
Source class to be adapted:
// PowerSouce. javapackage org. uestc. adapter; public class PowerSource {private int voltage; public void SetDianya (int v) {voltage = v; System. out. println ("the voltage I provided is:" + voltage);} public int GetVoltage () {return voltage ;}}
Adapter:
// Adapter. javapackage org. uestc. adapter; import org. uestc. adapter. powerSource; public class Adapter {private PowerSource powerSource; public Adapter (PowerSource powerSource) {this. powerSource = powerSource;} public int voltageSwitch () {int intpuDianya = powerSource. getVoltage (); System. out. println ("start voltage conversion now ..... "); int outputDianya = 5; return outputDianya ;}}
Target class:
// Arm. javapackage org. uestc. adapter; public class Arm {private int voltage; public void inputDianya (int v) {voltage = v;} public void run () {if (voltage = 5) {System. out. println ("arm running properly");} else {System. out. println ("the input voltage is abnormal and cannot be started ");}}}
Client test:
//client.javapackage org.uestc.adapter;import org.uestc.adapter.PowerSource;;public class Client {public static void main(String []args) {PowerSource powerSource = new PowerSource();powerSource.SetDianya(220);Adapter adapter = new Adapter(powerSource);Arm arm = new Arm();arm.inputDianya(adapter.voltageSwitch());arm.run();}}
Running result:
The voltage I provided is: 220, and now the voltage conversion is started. arm runs normally.