Contents of article series (continuously updated) :

Contents of article series (continuously updated) :

Chapter 1: Overview, Coupling, UML, Seven Principles, Detailed Analysis and summary (Based on Java)

Chapter two: Several implementations of singleton pattern And its destruction by reflection

【 Design Pattern 】 Chapter 3: A Simple factory, factory method pattern, abstract factory pattern 】

The Builder model is not that difficult

Chapter 5: What is a Prototype Pattern? Shallow fetch shallow copy and deep copy

Article 6: Look at the adapter pattern

[Design Pattern] Chapter 7: Understand the bridge pattern with me

[Design mode] Chapter 8: Drinking soy milk is decorator mode?

[Design Pattern] Chapter 9: Combination pattern to solve the problem of hierarchical structure

[Design mode] Chapter 10: Appearance mode, the joy of driving a small broken car

[Design mode] Chapter 11: Let’s take a look at the yuan model

[Design Mode] Chapter 12: explanation of agent mode in the ticket purchase scene

A quote

Design mode is an idea, a design way, which can help us to deal with some problems (not necessarily technology, such as problems in life) to provide some ideas, as well as the modeling and description of problem solutions

For example, the adaptor pattern we are going to talk about today is a transition from solving life problems to technical problems

In real life, often can appear not compatible, both to go abroad, for example, our plug from domestic and foreign the socket is not match, for example, I travel to Hong Kong again, but can’t speak cantonese, and some of the shops on a little, you need a friend who can speak cantonese to help me in the middle

On the technical side, some of the components that we are developing for some business already exist in the library, but because we have been developing components in the past that are not compatible with the current interface specification, using the adapter pattern is to find an intermediate quantity that helps us adapt the two and connect them together, so that we do not have to develop a new component

Ii code Demo

The background of the example is as follows: We now have an iPhone (Lightning port), but now we only have one Type-C charging cable. We need to find a way to add an intermediate adapter so as to achieve the effect of charging the phone

(a) Class adapter (inherited, not commonly used)

(1) demonstration

Although this one is not commonly used, but the second kind is improved on the basis of the first kind, the content is not a lot, can be patient to see oh

First, define an iPhone class that has a method for charging the phone.

/** * Client class: the iPhone wants to charge, but the charging cable head is type-c */
public class Phone {
    // Charging method
    public void charging(Converter converter){ converter.TypeCToLightning(); }}Copy the code

Type-c charging line is then defined, because it cannot be inserted into the Lightning port of iPhone, so it is the adapted class

/** * The class to be adapted: type-c charging cable */
public class TypeCLine {

    public void charging(a){
        System.out.println("It's starting to charge."); }}Copy the code

We have defined two contents that cannot be directly related, namely mobile phone and Type-c charging cable. Now there is only one intermediate adapter missing. Now we can define a conversion interface, in which a method of type-c to Lightning can be defined

/** * Charger conversion interface */
public interface Converter {
    // Type -c converts to Apple Lightning port
    void TypeCToLightning(a);
}
Copy the code

Next is a concrete implementation of the class, the implementation of the abstract interface is nothing to say, there is a step is the key — inherit the charging line this needs to be adapted to the class, as for its advantages and disadvantages we talk about below

/** * Type-c charging cable converter */
public class TypeCLineAdapter extends TypeCLine implements Converter {
    @Override
    public void TypeCToLightning(a) {
        // The connection is successful. You can use the converted type-c cable to charge the iPhone
        super.charging(); }}Copy the code

Test the

public class Test {
    public static void main(String[] args) {
        Phone phone = new Phone(); / / cell phone
        TypeCLine typeCLine = new TypeCLine(); // Type -c charging cable
        Converter typeCLineAdapter = new TypeCLineAdapter(); // Type -c charging cable adapter
        // The phone is plugged directly into the adaptor to chargephone.charging(typeCLineAdapter); }}Copy the code

Result: Start charging

(2) the advantages and disadvantages

In the test code, you can see that typeCLine doesn’t seem to be used, and it is, because in the adapter class, the TypeCLineAdapter class, we inherit typeCLine directly from the type-c charging cable class, that is, Its concept is realized through multiple inheritance match between two interfaces, but like c #, VB.NET, Java and other languages are not support multiple inheritance, and the inheritance, inheritance in the case of control inappropriate will always bring us some trouble, so here is not very appropriate, so have the following way

(2) Object adapter (combination, common)

(1) demonstration

Cell phone and Type-C charging cable this is unchanged, all you need to do is modify the specific adapter class TypeCLineAdapter, so here we create a new TypeCLineAdapter2

/** * Type-c charging cable converter */
public class TypeCLineAdapter2 implements Converter {

    private TypeCLine typeCLine;

    public TypeCLineAdapter2(TypeCLine typeCLine) {
        this.typeCLine = typeCLine;
    }

    @Override
    public void TypeCToLightning(a) {
        // The connection is successful. You can use the converted type-c cable to charge the iPhonetypeCLine.charging(); }}Copy the code

Test it out:

public class Test {
    public static void main(String[] args) {
        Phone phone = new Phone(); / / cell phone
        TypeCLine typeCLine = new TypeCLine(); // Type -c charging cable
        Converter typeCLineAdapter2 = new TypeCLineAdapter2(typeCLine); // Type -c charging cable adapter
        // The phone is plugged directly into the adaptor to chargephone.charging(typeCLineAdapter2); }}Copy the code

(2) the advantages and disadvantages

In the TypeCLineAdapter2 step, we first undid the adapter inheritance of the TypeCLine class, choosing instead to call it in a method by adding a TypeCLine private domain to the adapter TypeCLineAdapter2 class

First of all, we get rid of the trouble of single inheritance, so what good is it

In the book Effecttive Java, chapter 4, Article 18: Composition Over Inheritance, there is a better one, which I have briefly summarized:

We can create a new class (the TypeCLineAdapter2 class) and add a private domain (the TypeCLine class) to the new class that references an instance of an existing class. This design is called composition

  • Therefore, the existing class becomes a component of the new class, and each instance method in the new class can call the corresponding method contained in the existing class, and return his result, which is called forward, and the method in the new class is called forward method

Summary: In this way, the test will feel more comfortable, as we thought, the phone and type-C line, respectively through the connection of the adapter, can be adapted to the way of charging, but also avoid the use of inheritance, which is a better way to deal with the adaptation problem

Three adaptor pattern theory

(1) Definition and classification

Adapter pattern definition: to convert the interface of one class into another interface that the customer expects, so that classes that would not otherwise work together due to interface incompatibility can work together

Classification:

  • Adapter-like pattern

    • Mainly using inheritance implementation, high coupling degree, and in a single inheritance language will be limited, but also need to prevent some problems caused by inheritance
  • Object adapter pattern

    • A composition (or composition) approach is used to reduce coupling and is recommended

(2) Structure

① Class adapter pattern

② Object adapter pattern

Again, analyze the characters:

Adaptee class: It is a class that needs to be adapted, such as the type-C cable mentioned above, before it can be plugged into an iPhone to charge

Target interface: the interface expected by the current system business, which can be an abstract class or interface corresponding to the Converter interface in the code, which is the desired adaptation conversion interface of the customer

Adapter class: It is a converter that converts the Adapter interface into the target interface by inheriting or referencing the Adapter’s object, allowing customers to access the Adapter in the format of the target interface