[Dahua design mode] mode 5: adapter mode

[import]

The word "adaptation" should be familiar. It often appears in electricity. The family voltage is 220V, but our electrical appliances, such as mobile phones, have a low charging rated voltage, and the charging voltage of computers is also different. However, we need to use the same power supply, and the adapter can turn the power supply into the required voltage. This is the power adapter.

1, Adapter mode

The Adapter pattern transforms the interface of a class into another interface that the customer wants. The Adapter pattern allows classes that cannot work together because of incompatible interfaces to work together.                 

Two, pattern classification and Implementation:

1. Object adapter: loose coupling

UML diagram:

[code implementation]

Adaptee (adapted class)

public class Adaptee {
    public void SpecialRequest(){
        System.out.println("Special request!");
    }
}

Adapter

public class Adapter extends Target {
    private Adaptee adaptee;

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

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

Target (interface expected by customers)

public class Target {
    public void request(){
        System.out.println("Ordinary request");
    }
}

Client class

public class Client {
    public static void main(String[] args) {
        Target target=new Adapter(new Adaptee());
        target.request();
    }
}

2. Class adapter: high coupling

UML diagram:

[code implementation]

Adaptee (adapted class)

public class Adaptee {
    public void specialRequest(){
        System.out.println("Special request!");
    }
}

Adapter

public class Adapter extends Adaptee implements Target {
    @Override
    public void request() {
        specialRequest();
    }
}

Target (interface expected by customers)

public interface Target {
    public void request();
}

Client class

public class Client {
    public static void main(String[] args) {
        Target target=new Adapter();
        target.request();
    }
}

Summary:

The biggest difference between class adapter mode and object adapter mode is that the relationship between adapter and adapter is different. In object adapter mode, adapter and adapter are associated, while in class adapter mode, adapter and adapter are inherited. Moreover, because the class adapter matches an interface with another interface through multiple inheritance, and languages such as Java do not support multiple inheritance, the multi-purpose object adapter is used.

The adapter pattern decouples the target class from the adapter class, and reuses the existing adapter class by introducing an adapter class without modifying the original structure, which increases the transparency and reusability of the class.
Applicable scenario: the system needs to use some existing classes, and the interfaces of these classes (such as method names) do not meet the needs of the system, or even have no source code of these classes.

3, Scenario example

Now there is a ready-made SortUtil class that can sort integers and sort numeric strings through the adapter.

SortUtil class (adapted class)

public class SortUtil {
    public List sort(List<Integer> list){
        Collections.sort(list);
        return list;
    }
}

Target class (interface desired by the customer)

public interface Target {
    public List sortString(List<String> list);
}

SortAdapter class

/**
 * object adapter 
 */
public class SortAdapter implements Target {
    private SortUtil sortUtil;
    public SortAdapter(SortUtil sortUtil){
        this.sortUtil=sortUtil;
    }
    @Override
    public List sortString(List<String> oldList) {
        List<Integer> integerList1 = new ArrayList<Integer>();
        List<String> newList = new ArrayList<String>();
        for (String string : oldList) {
            integerList1.add(Integer.parseInt(string.replaceAll("[^0-9]*", "")));
        }
        sortUtil.sort(integerList1);
        for (Integer integer : integerList1) {
            for (String stringList1 : oldList)
                if (stringList1.contains(integer.toString())) {
                    newList.add(stringList1);
                }
        }
        return newList;
    }
}

4, Application of adapter mode in JDK

In java JDK, there are many usage scenarios of adapter mode, such as Java. Net in collection package util. Arrays #aslist(), Java in io package io. InputStreamReader(InputStream),java.io.OutputStreamWriter(OutputStream), etc.

Reader class corresponds to clientarget abstract class, InputStreamReader class corresponds to Adapter class, InputStream class corresponds to Adaptee, and reader corresponds to Target. InputStreamReader , adapts the reader , interface to the InputStream , interface.

If the article is useful to you, support it for three times!!!

Keywords: Java Design Pattern

Added by lokie538 on Thu, 27 Jan 2022 20:56:25 +0200