5. Adapter Mode

Adapter mode

First, the basic introduction

1) Adapter mode (Adapter Pattern) converted into an interface, a client interface represents another desired aim of compatibility.

2) structural pattern belongs Adapter Model

3) divided into three categories: Class Adapter mode, the object adapter mode, the interface adapter mode

Second, the model-based adapter

1. Class Adapter Mode

Basic Introduction: Adapter class, the integrated type src, dst class interface to achieve complete src-> dst adaptation

2. Class Adapter Model Application Examples

1) Application Examples:

Life examples to explain the charger, the charger itself is equivalent Adapter, 220V AC equivalent src (adapted by), dst goal is 5V DC

2) Analysis of FIG.

3) code implementation

public class Client {

    public static void main(String[] args) {
        System.out.println(" === 类适配器模式 ====");
        Phone phone = new Phone();
        phone.charging(new VoltageAdapter());
    }
}
//适配接口
public interface IVoltage5V {
    public int output5V();
}
//被适配的类
public class Voltage220V {
    //输出220V的电压
    public int output220V() {
        int src = 220;
        System.out.println("电压=" + src + "伏");
        return src;
    }
}
//适配器类
public class VoltageAdapter extends Voltage220V implements IVoltage5V {
    @Override
    public int output5V() {
        //获取到220V电压
        int srcV = output220V();
        int dstV = srcV / 44 ; //转成 5v
        return dstV;
    }
}
public class Phone {
    //充电
    public void charging(IVoltage5V iVoltage5V) {
        if(iVoltage5V.output5V() == 5) {
            System.out.println("电压为5V, 可以充电~~");
        } else if (iVoltage5V.output5V() > 5) {
            System.out.println("电压大于5V, 不能充电~~");
        }
    }
}

3. Notes and Class Adapter pattern details

1) java is a single inheritance, all classes need to inherit src adapter class can be considered a drawback because it requires dst must be an interface, there are certain limitations

2) A method in src Adapter class will be exposed, increasing the cost of use.

Third, the object adapter mode

1. Basic Introduction

1) with the same class adapter mode, but will make changes Adapter class, not inherited src class, second class hold src instance, it has been resolved compatibility issues. Namely: to hold src class that implements the class interface dst

2) The "synthetic multiplexing principle", make use of the association relationship in the system (polymerization) instead of inheritance

3) object adapter mode adapter is commonly used as a model

2. FIG. Analysis (only need to modify the adapter)

3. code implementation

//适配器类
public class VoltageAdapter  implements IVoltage5V {

    private Voltage220V voltage220V; // 关联关系-聚合

    //通过构造器,传入一个 Voltage220V 实例
    public VoltageAdapter(Voltage220V voltage220v) {
        this.voltage220V = voltage220v;
    }

    @Override
    public int output5V() {
        int dst = 0;
        if(null != voltage220V) {
            int src = voltage220V.output220V();//获取220V 电压
            System.out.println("使用对象适配器,进行适配~~");
            dst = src / 44;
            System.out.println("适配完成,输出的电压为=" + dst);
        }
        return dst;
    }
}

4. Notes and details

The synthetic multiplexing principle, a combination of inherited Alternatively, the adapter must be solved class inherits the limitations of src, dst must be no longer requires the interface

Fourth, the interface adapter mode

1. The core ideas:

When no interface provides a method to achieve full, it may be designed to implement an abstract class interface, and implement a (null Method) The default method for each interface, the abstract class is a subclass of selective coverage parent some ways to achieve demand.

Figure 2. Analysis

3. code implementation

public interface Interface4 {
    public void m1();
    public void m2();
    public void m3();
    public void m4();
}
//在AbsAdapter 我们将 Interface4 的方法进行默认实现
public abstract class AbsAdapter implements Interface4 {
    //默认实现
    public void m1() {
    }
    public void m2() {
    }
    public void m3() {
    }
    public void m4() {
    }
}
public class Client {
    public static void main(String[] args) {
        AbsAdapter absAdapter = new AbsAdapter() {
            //只需要去覆盖我们 需要使用 接口方法
            @Override
            public void m1() {
                System.out.println("使用了m1的方法");
            }
        };
        absAdapter.m1();
    }
}

Five, SpringMVC Framework application source code analysis

1) SpringMvc in HandleAdapter, using the adapter pattern

2) The reason for using HandlerAdapter analysis:

We can see the different types of processors, have multiple implementations, the call is not a way to determine if the direct method calls the controller, you have to use when you need to call if else to determine which type is then to be performed, followed by extension would have to modify the original code.

3) source code analysis

4) Analysis of FIG source class

VI Notes and details of the Adapter pattern

1) three named, is based on src is what form to give Adapter named.

2) class adapter: In the Adapter in the src as classes, inheritance

Object Adapter: the src as an object, hold

Interface adapter: the src as an interface,

Guess you like

Origin www.cnblogs.com/chao-zjj/p/11299990.html