Java Design Pattern Series (6) Adapter Pattern

Java Design Pattern Series (6) Adapter Pattern

The adapter pattern transforms the interface of one class into another interface expected by the client, so that two classes that would otherwise not work together due to interface mismatches can work together.

The structure of the adapter pattern:

  1. Adapter pattern for classes
  2. Object Adapter Pattern

1. Class Adapter Mode

The class adapter pattern transforms the API of the adapted class into the API of the target class.

Figure 6-1 Adapter pattern for classes

As you can see in the image above, the Adaptee class does not have a sampleOperation2() method, which the client expects. In order to enable the client to use the Adaptee class, an intermediate link, namely the class Adapter, is provided to connect the API of the Adaptee and the API of the Target class. Adapter and Adaptee have an inheritance relationship, which determines that this adapter pattern is a class:

The roles involved in the mode are:

(1) Target role: This is the expected interface. Note: Since the class adapter pattern is discussed here, the target cannot be a class.

(2) Source (Adapee) role: the interface that needs to be adapted now.

(3) Adapter (Adaper) role: The adapter class is the core of this mode. The adapter converts the source interface to the target interface. Obviously, this role cannot be an interface, but must be a concrete class.

source code

public interface Target {

    /** 这是源类Adaptee也有的方法 */
    public void sampleOperation1();

    /** 这是源类Adapteee没有的方法 */
    public void sampleOperation2();
}

Given above is the source code of the target role, which is implemented in the form of a JAVA interface. As you can see, this interface declares two methods: sampleOperation1() and sampleOperation2(). While the source role Adaptee is a concrete class, it has a sampleOperation1() method, but no sampleOperation2() method.

public class Adaptee {

    public void sampleOperation1(){}
}

The adapter role Adapter extends Adaptee and implements the Target interface at the same time. Since Adaptee does not provide the sampleOperation2() method, and the target interface requires this method, the adapter role Adapter implements this method.

public class Adapter extends Adaptee implements Target {

    /**
     * 由于源类 Adaptee 没有方法 sampleOperation2()
     * 因此适配器补充上这个方法
     */
    @Override
    public void sampleOperation2() {
        //写相关的代码
    }
}

2. Object Adapter Pattern

Like the adapter mode of the class, the adapter mode of the object converts the API of the adapted class into the API of the target class. Unlike the adapter mode of the class, the adapter mode of the object does not use inheritance to connect to the Adaptee class, but Connect to the Adaptee class using delegation.

Figure 6-1 Adapter pattern for objects

public interface Target {

    /** 这是源类Adaptee也有的方法 */
    public void sampleOperation1();

    /** 这是源类Adapteee没有的方法 */
    public void sampleOperation2();
}

public class Adaptee {

    public void sampleOperation1(){}
}

public class Adapter implements Target {

    private Adaptee adaptee;

    public Adapter(Adaptee adaptee){
        this.adaptee = adaptee;
    }

    /** 源类 Adaptee 有方法 sampleOperation1,因此适配器类直接委派即可 */
    public void sampleOperation1(){
        this.adaptee.sampleOperation1();
    }

    /** 源类 Adaptee 没有方法 sampleOperation2,因此由适配器类需要补充此方法 */
    public void sampleOperation2(){
        //写相关的代码
    }
}

It is recommended to use the implementation of object adapters as much as possible, using more synthesis/aggregation and less inheritance.

3. The structure of the default adaptation mode

The default adapter mode is a "trivial" adapter mode.

public interface AbstractService {
    public void serviceOperation1();
    public int serviceOperation2();
    public String serviceOperation3();
}
public class ServiceAdapter implements AbstractService{

    @Override
    public void serviceOperation1() {
    }

    @Override
    public int serviceOperation2() {
        return 0;
    }

    @Override
    public String serviceOperation3() {
        return null;
    }

}

It can be seen that the interface AbstractService requires the definition of three methods, namely serviceOperation1(), serviceOperation2(), and serviceOperation3(); and the abstract adapter class ServiceAdapter provides mediocre implementations for these three methods. Therefore, any concrete class that inherits from the abstract class ServiceAdapter can choose to implement the methods it needs, regardless of other methods that are not needed.

The purpose of the adapter pattern is to change the interface of the source so that the interface of the destination is compatible. The purpose of the default adapter is slightly different, it is a trivial implementation provided to facilitate the creation of a non-trivial adapter class.

At any time, if you are not going to implement all the methods of an interface, you can use the "default adaptation pattern" to make an abstract class that gives a trivial concrete implementation of all the methods. In this way, subclasses that inherit from this abstract class do not have to implement all the methods.

Advantages and Disadvantages of Adapter Mode

Advantages of Adapter Pattern

  1. Better reusability The
      
    system needs to use existing classes, and the interface of this class does not meet the needs of the system. Then these functions can be better reused through the adapter mode.

  2. better scalability

    When implementing the adapter function, you can call the function developed by yourself, so as to naturally expand the function of the system.

Disadvantages of Adapter Pattern

Excessive use of adapters will make the system very messy and difficult to grasp as a whole. For example, it is obvious that the A interface is called, but it is actually adapted to the implementation of the B interface. If there are too many such situations in a system, it is tantamount to a disaster. Therefore, if it is not necessary, you can directly refactor the system without using the adapter.


Record a little bit every day. Content may not be important, but habits are!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325694072&siteId=291194637