Concept:
The adapter mode, also known as the adapter mode, is one of the stereotype patterns that can change the interface form of an existing class (or external Class) through the adapter mode.
For example: We use the computer, the home power is 220V, and our computer is 18V, then if we directly connect the power to the computer, will certainly lead to computer burnout, because the power supply voltage is too high, then we need a power adapter, connected between the power and the computer, through the adapter to carry out a buck To ensure that the computer is working properly.
Add Adapter
Implemented in code:
First, if you don't use an adapter,
Create a new 220V power supply
1 // 220V power supply 2 Public class powersupply {3 Public void powersupply220v () {4 System.out.println ("Use 220V power supply") ; 5 }6 }
Create a new laptop, use power
1 // laptop uses 220V power supply 2 Public class Computer {3 Public Static void Main (string[] args) {4 New powersupply (); 5 powersupply.powersupply220v (); 6 }7 }
The results are as follows:
So the laptop directly uses the 220V voltage, but in this case, the laptop will burn directly, can not be used, because the voltage is too high, so we need to connect an adapter in the middle to achieve the purpose of buck
Adapter Inherits 220V Voltage
1 //Adapter2 Public classAdapterextendspowersupply{3 Public voidpowersupply18v () {4SYSTEM.OUT.PRINTLN ("Use adapter");5 This. powersupply220v ();6SYSTEM.OUT.PRINTLN ("Voltage drop to 18V");7 }8}
Laptop calls power through adapter
// laptop computer with 220V power supply via adapter Public class Computer { publicstaticvoid main (string[] args) { new Adapter (); adapter.powersupply18v (); }}
The results are as follows:
As can be seen, in this form, we use the previous power supply, but through the adapter, the voltage is reduced to 18V, the computer can be used normally.
But this is just one form of the adapter pattern.
The following is a more detailed description of the adapter mode
Adapter Mode Usage Scenarios
In a large-scale system development process, we often encounter situations such as the following:
We need to implement some features that already have one or more external components that are less mature, and if we re-develop them ourselves will take a lot of time, so in many cases we will choose to use the external components temporarily and then consider replacing them at any time. This, however, poses a problem, with the substitution of the external component library, which may require a large area of source code referencing the external component
Changes, so it is very likely to introduce new issues and so on. How to minimize the modification surface?
The adapter model is designed to address this similar demand. The adapter mode transparently calls an external component by defining a new interface (abstracting the functionality to be implemented) and a adapter (adapter) class that implements the interface. When you replace an external component, you can modify only a few adapter classes so that no other source code is affected.
In simple terms:
1, the system needs to use the existing classes, and these classes of interfaces do not meet the needs of the system.
2. Want to create a reusable class to work with some classes that are not too much related to each other, including some that might be introduced in the future.
3, need a unified output interface, and the input terminal type is unpredictable.
Let's talk about the structure of the adapter pattern
1. Implement adapter through inheritance
This form is the example we have just cited
Client: laptop computer (computer)
Target: is the method called in the laptop (adapter.powersupply18v ())
adaptee: 220V voltage (powersupply)
Adapter: adapter (Adapter)
2. Adoption of the Commission to achieve adapter
The second mode, just modify the adapter and the computer can
The adapter no longer inherits power but is treated as a member variable
1 //Adapter2 Public classadapter{3 Privatepowersupply powersupply;4 5 PublicAdapter (powersupply powersupply) {6 This. powersupply =powersupply;7 }8 9 Public voidpowersupply18v () {TenSYSTEM.OUT.PRINTLN ("Use adapter"); One This. powersupply.powersupply220v (); ASYSTEM.OUT.PRINTLN ("Voltage drop to 18V"); - } -}
Computer
1 // Laptops using 220V power supply via adapter 2 public class computer { 3 public static void main (string[] args) { 4 Adapter2 adapter = new Adapter2 (new powersupply ()); 5 adapter.powersupply18v (); 6 } 7 }
In general, use the second kind of commission to give more form, because this way does not have to inherit, use member variable more flexible
Java design mode-----15, Adapter mode