define

The adapter pattern makes it possible for two mismatched classes to work together by turning one class’s interface into another that the client expects

Usage scenarios

  • The system needs an existing class, but the interface of this class does not meet the requirements of the system, that is, the interface is incompatible.
  • You want to create a reusable class that works together with classes that are not very related to each other, including classes that may be introduced in the future.
  • A uniform output interface is required, but the type of input is unpredictable.

UML class diagrams

There are two kinds of adapter patterns, class adapter pattern and object adapter pattern

  1. Class adapter pattern
classDiagram
Target <|.. Adapter
Adaptee <|-- Adapter

class Target{
    <<interface>>
    +operation1()
    +operation2()
}
class Adapter{
    +operation2()
}
class Adaptee{
    +operation3()
}
  • Target The Target role, that is, the expected interface. Notice that this is not a class
  • Adaptee now needs the classes to be adapted
  • Adapter that ADAPTS existing classes to the desired interface

Class adapter simple instance

// The Target role requires 5V voltage
public interface FiveVolt{
    int getVolt5(a);
}

// Adaptee role, providing 220V voltage
public class Vlot220{
    pulic int getVlot220(a){
        return 220; }}// The adapter role converts the 220V voltage to 5V voltage
public class VlotAdapter extends Vlot220 implements FiveVolt{
    public int getVolt5(a){
        return 220to5Vlot();
    }
    
    private int 220to5Vlot(){
        return 5; }}/ / use
public class Test{
    public static void main(String[] args){
        VlotAdapter adapter = newVlotAdapter(); adapter.getVolt5(); }}Copy the code
  1. Object adapter
classDiagram
Target <|.. Adapter
Adaptee <.. Adapter

class Target{
    <<interface>>
    +operation1()
    +operation2()
}
class Adapter{
    +operation2()
}
class Adaptee{
    +operation3()
}

From the class diagram, we can see that the difference between class adapters and elephant adapters is mainly in the relationship between Adapter and Adaptee. The former is inheritance, the latter is composition.

Object adapter simple instance

/ / adapter
public class VoltAdapter implements FiveVolt{
    Volt220 mVolt220;
    public VoltAdapter(Volt220 adaptee){
        mVolt220 = adaptee;
    }
    
    public int getVolt5(a){
        return 220to5Vlot();
    }
    
    private int 220to5Vlot(){
        return 5; }}/ / use
public class Test{
    public static void main(String[] args){
        VlotAdapter adapter = new VlotAdapter(newVolt220()); adapter.getVolt5(); }}Copy the code

summary

This implementation directly bed the object to be adapted into the Adapter, using a combination of the form of interface compatibility. The adaptor mode is more flexible. Another advantage of the adaptor mode is that the methods of the adaptor will not be exposed. Because the adaptor mode inherits the adaptor, the methods of the adaptor will also be found in the Adapter class, which leads to some strange methods in the Adapter class. Therefore, object adapters are more flexible and practical.