One, the introduction

What is the adapter pattern? Why is there an adaptation mode? How to implement the adaptation mode?

  1. What is the adapter pattern? Translate the interface of a class into another interface that the customer wants. The adapter pattern makes it possible for classes to work together that would otherwise not work because of interface incompatibilities.
  2. Why do you have adapter patterns? For example, to buy a notebook, charging needs 20V voltage, but as we all know, our domestic voltage is 220V, so we need a power adapter to step-down. Adapters do the same.

Implement the adapter pattern

There are three ways to implement an adapter:

  1. The class adapter
  2. Object adapter
  3. Interface adapter

1. Class adapter, as follows:

As can be seen from the figure above, the adapter implements the adapter interface and inherits the 220V voltage. We are (Client). The Client needs 20V voltage, so we only need to take it from the adapter. The code is as follows:

/** * 220V voltage class */
public class V220 {
    public void pop220(a) {
        System.out.println("220V discharged."); }}//--------------------------------------------------------

/** * adapter interface */
public interface Adapter {
    public void pop(a);
}

/** * Adapter class * inherits from V220 and implements the adapter interface */
public class AdapterImpl extends V220 implements Adapter{
    public void pop(a) {
        super.pop220();
        System.out.println("Adapter receives 220V voltage to 20V, output"); }}/ / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the Main method -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
public static void main(String[] args) {
        AdapterImpl adapter = new AdapterImpl();
        // Take 5V from the adapter
        adapter.pop();
}
Copy the code

The running results are as follows:The code above is very simple,The main idea is to express the idea that if you cannot call the desired method (POP220), you can use the adapter to add a layer in the middle of the call.

I don’t know if you remember the seven principles of design pattern, the composite reuse principle. While we are in the adapter, the adapter inherits the V220 class, and we can actually call the POP220 method in an aggregate manner. This approach is called an object adapter.

2. Object adapter, as follows:

A class diagram is similar to a class adapter in that it changes inheritance to aggregation and reduces coupling. The code is as follows:

*/ public class AdapterImpl implements Adapter{private V220 V220; public AdapterImpl(V220 v220){ this.v220=v220; } public void pop() { v220.pop220(); System.out.println(" adapter receives 220V to 20V, output "); }} / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the Main method -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the public static void Main (String [] args) {V220 V220 = new V220(); // AdapterImpl adapter = new AdapterImpl(v220); // Get 5V adapter.pop(); }Copy the code

The result is the same. The object adapter differs from the class adapter in that the class adapter is an inherited call and the object adapter is an aggregate call.

3. Interface adapter

Interface adapters can be applied to an interface in many ways, but they can be used without all implementation, which is equivalent to meeting the interface isolation principle. The class diagram is as follows:Looking at the diagram is confusing, now let’s code the following:

/** * voltage interface (a total of 3 methods) */
public interface Voltage {
    public void V5(a);
    public void V20(a);
    public void V220(a);
}

//-------------------------------------------------------

/** * Voltage abstract class (implements voltage interface) * where the interface method is empty implementation */
public  abstract class VoltageImpl implements Voltage {
    public void V5(a) {}public void V20(a) {}public void V220(a) {}}//----------------------------Main------------------------------
public static void main(String[] args) {

        // Implement the abstract class
        VoltageImpl voltage = new VoltageImpl() {
            public void V5(a) {
                System.out.println("Output voltage 5V"); }}; voltage.V5();/ / output 5 v
        
        //--------------------------------------------------
        // Implement the abstract class
        VoltageImpl voltage1 = new VoltageImpl() {
            public void V20(a) {
                System.out.println("Output voltage 20V"); }}; voltage1.V20(); }Copy the code

The output is:As you can see,The voltage interface has three methods, we write an abstract class to implement it, and then empty implement all the methods, and then need any method to implement the abstract class, this is the interface isolation adapter.

Third, the end

These are the three implementations of the adapter, each with its own application scenario.