Adapter pattern of design pattern

1. Basic introduction

  • definition

    Convert the interface of a class into another interface desired by the customer, so that those classes that cannot work together due to incompatible interfaces can work together.

  • Main solution

    The main solution is that in the software system, some "existing objects" are often put into the new environment, and the interface required by the new environment cannot be met by the existing objects.

  • When to use

    1. The system needs to use existing classes, and such interfaces do not meet the needs of the system.
    2. You want to create a reusable class to work with some classes that are not closely related to each other, including some classes that may be introduced in the future. These source classes do not necessarily have consistent interfaces.
    3. Insert one class into another class family through interface conversion.
  • How to solve

    Inheritance or dependency (dependency is recommended)

  • critical code

    The adapter inherits or relies on existing objects to implement the desired target interface.

  • advantage

    1. You can let any two classes that are not associated run together.
    2. Class reuse is improved.
    3. Increased class transparency.
    4. Good flexibility.
  • shortcoming

    1. 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 in fact, it is internally adapted to the implementation of 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 reconstruct the system directly without using the adapter.
    2. Since JAVA inherits at most one class, it can only adapt to one adapter class at most, and the target class must be an abstract class.
  • Usage scenario

    If you are motivated to modify the interface of a functioning system, you should consider using the adapter mode.

  • matters needing attention

    Adapters are not added during detailed design, but solve the problems of projects in service.

  • Application examples

    1. American electric appliance is 110V and Chinese electric appliance is 220V. There must be an adapter to convert 110V to 220V.

    2. JDK 1.1 provides the Enumeration interface, while 1.2 provides the Iterator interface. If you want to use JDK 1.2, you need to convert the Enumeration interface of the previous system into the Iterator interface. At this time, you need the adapter mode.

    3. JDBC in java. JDBC provides a general abstract interface for the client, and every specific database manufacturer will develop jdbc driver, which is an adapter software between JDBC interface and database engine interface.

2. Classification and use

  • Basic introduction
    • The adapter pattern acts as a bridge between two incompatible interfaces.
    • The adapter mode is a structural mode.
    • It is mainly divided into three categories: class adapter mode, object adapter mode and interface adapter mode.
  • Structural role
    • Target interface: the interface expected by the current system business. It can be an abstract class or interface
    • Adapter class: it is the component interface in the existing component library that is accessed and adapted.
    • Adapter class: it is a converter that converts the adapter interface into a target interface by inheriting or referencing the adapter object, so that customers can access the adapter in the format of the target interface.

Type 2.1 adapter mode

The implementation of class adapter mode is to let the adapter class inherit the adapter class and implement the target interface. When rewriting the method of the target interface, the internal call is actually the method of the adapter class.

Structure diagram:

Target interface

/**
 * Target interface
 */
public interface Target {
    public void request();
}

Adapter class

/**
 * Adapter class
 */
public class Adaptee {
    public void specificRequest(){
        System.out.println("The business code in the adapter is called");
    }
}

Class adapter class

/**
 * Class adapter class
 */
public class ClassAdapdter extends Adaptee implements Target{
    @Override
    public void request() {
        super.specificRequest();
    }
}

Test class (client)

@Test
public void test01(){
    Target target = new ClassAdapdter();
    target.request();
}

result

The business code in the adapter is called
  • Class adapter mode benefits
    • Since the adapter class is a subclass of the adapter class, some adapter methods can be replaced in the adapter class, making the adapter more flexible.
  • Class adapter mode disadvantages
    • For languages that do not support multiple inheritance, such as Java, only one adapter class can be adapted at a time, and multiple adapters cannot be adapted at the same time.
    • The methods of the adapter class will be fully exposed in the adapter class, which increases the cost of use.

2.2 object adapter mode

The difference between the object adapter mode and the class adapter is that the object adapter mode implements the adaptation in a combined way. The adapter class implements the target interface, but does not inherit the adapter class. An adapter instance is defined in the adapter, and the method of the adapter instance is called when realizing the method of the target interface.

Structure diagram:

Target interface

/**
 * Target interface
 */
public interface Target {
    public void request();
}

Adapter class

/**
 * Adapter class
 */
public class Adaptee {
    public void specificRequest(){
        System.out.println("The business code in the adapter is called");
    }
}

Object adapter class

/**
 * Object adapter class
 */
public class ObjectAdapter implements Target{

    private Adaptee adaptee;

    public ObjectAdapter(Adaptee adaptee){
        this.adaptee = new Adaptee();
    }

    @Override
    public void request() {
        adaptee.specificRequest();
    }
}

Test class (client)

    @Test
    public void test02(){
        Adaptee adaptee = new Adaptee();
        Target target = new ObjectAdapter(adaptee);
        target.request();
    }

result

The business code in the adapter is called
  • Advantages of object adapter mode
    • An object adapter can adapt multiple different adapters to the same target.
    • Subclasses of adapters that can be adapted.
    • It solves the defect that the adapter class must inherit the adapter class.
    • Lower cost and more flexible.
  • Disadvantages of object adapter pattern
    • The implementation process of replacing the adapter is complex

2.3 interface adapter mode

We can use the interface adapter pattern when we only want to use some methods in the interface instead of all methods. The interface adapter pattern is to let the adapter class implement all the methods of the target interface, but there is nothing inside the method. When we only need to use part, we can use the subclass of the adapter class to rewrite the methods we need.

Structure diagram:

Target interface

/**
 * Target interface
 */
public interface Target {
    public void method01();
    public void method02();
    public void method03();
    public void method04();
}

Interface adapter class

/**
 * Interface adapter class
 */
public class InterfaceAdapter implements Target {

    @Override
    public void method01() {

    }

    @Override
    public void method02() {

    }

    @Override
    public void method03() {

    }

    @Override
    public void method04() {

    }
}

Subclass of interface adapter class

/**
 * Interface adapter class subclass
 * Suppose you only need to rewrite method01() and method02()
 */
public class InterfaceAdapterSon extends InterfaceAdapter{

    @Override
    public void method01() {
        System.out.println("Subclass override of interface adapter class method01()method");
    }

    @Override
    public void method02() {
        System.out.println("Subclass override of interface adapter class method02()method");
    }
}

Test class (client)

@Test
public void test01(){
    Target target = new InterfaceAdapterSon();
    target.method01();
    target.method02();
}

result

Subclass override of interface adapter class method01()method
 Subclass override of interface adapter class method02()method

3. Summary

  • Advantages of adapter mode

    • Decouple the target class from the adapter class, and use the existing adapter class by introducing an adapter class without modifying the original code (in line with the opening and closing principle).
    • Through the adapter, the client can call the same interface, and the specific implementation business is encapsulated in the adapter class, which is transparent to the client.
    • Reusing existing classes solves the problem of inconsistency between existing classes and reuse environment requirements. The reusability of adapter class is improved. The same adapter class can be reused in multiple different systems.
  • Application scenario

    • The system wants to use existing classes, and the interfaces of these classes do not conform to the interfaces of the system.
    • When two classes do the same or similar things, but have different interfaces.
    • When using third-party components, the component interface definition is different from that defined by yourself. You do not want to modify your own interface, but you should use the functions of third-party components.

Keywords: Java Design Pattern

Added by titoni on Sat, 29 Jan 2022 21:21:06 +0200