8 - Design Patterns - Adapter Pattern

Reference to the following

https://blog.csdn.net/zxt0601/article/details/52848004
https://blog.csdn.net/qq924862077/article/details/53399969

Adapter Pattern Concepts

The following is excerpted from the rookie tutorial

Intent: Convert the interface of a class to another interface that the client wants. The adapter pattern enables classes to work together that would otherwise not work together due to incompatible interfaces.

Main solution: The main solution is that in the software system, some "existing objects" are often placed in a new environment, and the interface required by the new environment cannot be satisfied by the existing objects.

When to use:
1. The system needs to use the existing class, and the interface of this class does not meet the needs of the system.
2. Want to create a reusable class to work with some classes that are not much related to each other, including some classes that may be introduced in the future, these source classes do not necessarily have a consistent interface.
3. Insert a class into another class system through interface conversion. (For example, tigers and birds, there is now a flying tiger. Without increasing the needs of entities, an adapter is added, which contains a tiger object and implements the interface of flying.)

How to fix: inheritance or dependency (recommended).

Key code: Adapter inherits or relies on existing objects to achieve the desired target interface.

Application examples:
1. American electrical appliances 110V, China 220V, there must be an adapter to convert 110V to 220V.
2. JAVA JDK 1.1 provides the Enumeration interface, while in 1.2 it provides the Iterator interface. If you want to use the JDK 1.2, you need to convert the Enumeration interface of the previous system into an Iterator interface, then you need the adapter mode.
3. Run the WINDOWS program on LINUX.
4. JDBC in JAVA.

Advantages:
1. Any two unrelated classes can be run together.
2. Improve the reuse of classes.
3. Increase the transparency of the class.
4. Good flexibility.

Disadvantages:
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 it is actually adapted to the implementation of the B interface. If a system has too many such situations, it is tantamount to a disaster. Therefore, if it is not necessary, you can directly refactor the system 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.

Use case: Motivated to modify the interface of a functioning system, you should consider using the adapter pattern.

Note: Adapters are not added at the time of detailing, but rather to address issues with in-service items.

Adapters are divided into three categories:

  • Class adapter: The adapter class inherits the class that needs to be adapted
  • Object adapter: The adapter class has a composition relationship with the class that needs to be adapted (understanding of the composition relationship 1-design pattern-concept )
  • Interface adapter: The adapter class implements the interface that needs to be adapted

class adapter

The adaptee->adapter->target
adapter class inherits the adaptee class and implements the target interface

case

It is known that we have a household appliance whose plug is a three-hole socket, and now there is only a two-hole socket

Adaptee: The class that needs to be adapted

Two-hole socket, with getSocket() method, provides its two-hole socket to the outside

public class Adaptee {

    /**
     * 提供的插座
     * @return void
     * 时间:2018年4月25日
     */
    public int getSocket(){
        int socket = 2;
        System.out.println("二孔插座");
        return socket;
    }
}

Target: The target that the customer needs

Household appliance interface, which requires a three-hole socket for charging, with a charging method

public interface Target {

    /**
     * 充电
     * @return void
     * 时间:2018年4月25日
     */
    public void change();
}

Adapter: adapter

Adapter class, inherits Adaptee and implements Target interface

public class Adapter extends Adaptee implements Target{

    /**
     * 充电
     */
    @Override
    public void change() {
        int socket = getSocket();
        socket += 1;
        if (3 == socket) {
            System.out.println("充电...");
        }
    }

}

test class

public class Test {
    public static void main(String[] args) {
        Adapter adapter = new Adapter();
        adapter.change();
    }
}

From the above example, we can see that because Adapter (adapter) inherits Adaptee (object that needs to be adapted), our Target (target object) can only be an interface.

object adapter

The adaptee->adapter->target
adapter class combines the adaptee class to implement the target interface

case

It is known that we have a household appliance whose plug is a three-hole socket, and now there is only a two-hole socket

Adaptee: The class that needs to be adapted

public class Adaptee2 {

    /**
     * 提供的插座
     * @return void
     * 时间:2018年4月25日
     */
    public int getSocket(){
        int socket = 2;
        System.out.println("二孔插座");
        return socket;
    }
}

Target: The target that the customer needs

public interface Target2 {

    public void change();
}

Adapter: adapter

public class Adapter2 implements Target2{

    private Adaptee2 adaptee2;

    public Adapter2() {
        adaptee2 = new Adaptee2();
    }

    @Override
    public void change() {
        int socket = adaptee2.getSocket();
        socket += 1;
        if (3 == socket) {
            System.out.println("充电...");
        }
    }

}

test class

public class Test2 {

    public static void main(String[] args) {
        Adapter2 adapter2 = new Adapter2();
        adapter2.change();
    }
}

interface adapter

The adaptee->adapter->target
adapter class implements the adaptee interface and the target interface at the same time

As for the case, see here for details

Design Patterns (2) Summary of Three Adapter Patterns and Usage Scenarios

Guess you like

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