adapter!

1. What is an adapter?

    Transformers are actually adapters.

    Convert an interface to the interface that the client needs. Extend functionality from one interface into another class. (The interface class does not only refer to the interface, this is narrow); make the interface classes work together because the interfaces are not compatible.

2. Adapter classification

    Multifunctional adapter: It is to create an Iworker interface, and connect to the workHandler through this interface to find the appropriate method invocation.

    Single-function adapter: It is through an Iwork interface, and through this interface is connected to the appropriate adapter, and the method is called through the adapter.

    The focus of these two adapters is the single-function adapter. Because of the single-function adapter used in the spring framework.

   Default adapter design pattern
    When an interface has a large number of abstract methods, if a class only needs to use a small part of the methods, and other methods do not need to be accessed, then there will be a problem:
the class that needs to be defined (implementing the interface) must implement all methods in this interface. Since most of these methods do not need access, they are implemented by empty implementation (only a pair of braces, no real method body content). This is more troublesome. At this point, you can define a general class and let this class empty implement all methods. Of course, you can also just empty the methods that are not used, and let the methods that you really want to use appear in the form of abstract methods. In this way, if you define the implementation class of the interface in the future, you do not need to directly implement the interface, but inherit this general class and override the method you want to actually access.
This program structure is called the default adapter design pattern.

3. What is a single function adapter?

public class Test {
public static void main(String[] args) {
Icooker cooker = new Cooker();
Iprogramn programmer = new programer();
Iteacher teacher = new teacherimple();Object[] workers = {cooker, programmer,teacher};for (Object worker : workers) {Iwork adapter = getAdapter(worker);adapter.work(worker);}}// 根据worker的具体类型查找与其相匹配的适配器private static Iwork getAdapter(Object worker) {Iwork ca = new cookerAdapter();Iwork pa = new programerAdapter();Iwork ta = new teacherAdapter();Iwork[] adapters = {ca, pa, ta};for (Iwork adapter : adapters) {if(adapter.support(worker)) {return adapter;}























}
return null;
}

}

//以下是创建的一个cookerAdapter
public class cookerAdapter implements Iwork{

@Override
public void work(Object worker) {
System.out.println(((Icooker) worker).cooker());;
}


@Override
public boolean support(Object worker) {
return worker instanceof Icooker;
}
}


4. What is a multifunction adapter?

    A multi-function adapter is a method of implementing multiple classes from multiple interfaces;

    Then a multi-function adapter interface converts these interfaces to another interface, and finally traverses in the test;

5. Multifunctional adapter code display

    



Guess you like

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