Introduce a,

1. Mode description

Simply put, define a wrapper class that wraps objects with incompatible interfaces.

2, Wrapped object = Adaptee= Adaptee class

2. Main functions

Change the interface of one class into another interface that the client expects, so that two classes can work together because their interfaces do not match. \

Second, mode principle

Class adaptor pattern

The adapter pattern for a class converts the API of the adaptive class into the API of the target class.

2. UML Class Diagram & Composition

      

From this picture we can see that:

Conflict: Target expects to call the Request method, while Adaptee does not.

To enable Target to use the SpecificRequest method in the Adaptee class, provide an intermediate Adapter class (Inherit Adaptee & to implement the Target interfaceLink the Adaptee API to Target’s API.

3. Take an example

A. Background: Xiao Bei bought a TV imported from the United States. The voltage required by the imported TV set (110V) is incompatible with the standard output voltage of the domestic plug (220V). Solution: Set an adapter to convert the 200V output from the plug into 110V

Step 1: Create Target interface (expected plug) : can output 110V (convert 220V to 110V);

Public interface Target {// Convert 110V (Adaptee) public void Convert_110v(); }Copy the code

Step 2: Wear the source type (the original plug);

Public void Output_220V() {}}Copy the code

Step 3: Create Adapter classes

Class Adapter220V extends PowerPort220V implements Target {// The desired plug requires Convert_110V, but the original plug does not. The adapter adds the method name // but in fact Convert_110V() knowledge calls the contents of the Output_220V() method of the original plug // so the adapter just wraps Output_220V(), @override public void Convert_110V() {this.output_220v; }}Copy the code

Step 4: Define the specific use target class and invoke the required methods through the Adapter class to achieve the target (no need to go through the original plug)

// ImportedMachine {@override public void Word() {system.out.printin (); // ImportedMachine {@override public void Word() {system.out.printin (); Public class AdapterPattern {public static void main(String[] args) {Target madapter220 = new Adapter220V(); ImportedMachine mImportedMachine = new ImportedMachine(); // The user takes the imported machine and plugs the adapter (calls Convert_110V() method) // Then plugs the adapter into the original plug (Convert_110V() method internally calls Output_220V() method output 220V) // The adapter is just a shell, provides 110V externally, But essentially 220V power supply madapter220v.convert_110V (); mImportedMachine.Work(); }}Copy the code

\

Three, advantages and disadvantages

1. Better reusability. The system needs to use existing classes whose interfaces do not meet the needs of the system, so the adapter pattern allows for better reuse of these functions.

Transparent and simple. The client can invoke the same interface and is therefore transparent to the client. It’s easier and more straightforward

Better scalability. When implementing the adapter functionality, you can invoke your own developed functionality, naturally extending the functionality of the system.

Decoupling. Decouple the target class from the adapter class, reusing the existing adapter class by introducing an adapter class without modifying the original code.

Open – close principle. The same adapter can adapt both the adapter class and its subclasses to the target interface; You can implement different adapters for different target interfaces without modifying the class to be adapted.

2, the shortcomings of too much use of adapters, will make the system very messy, not easy to grasp the whole.