Adapter mode (Adapter)----design mode

Adapter mode (Adapter)----design mode

What is it?

The Adapter Pattern acts as a bridge between two incompatible interfaces.

insert image description here

The adapter mode comes from life, for example, the card reader is used as the adapter between the memory card and the notebook, the current conversion socket is the adapter that solves the mismatch between the required voltage and the actual voltage, and so on.

A brief introduction to the adapter pattern

insert image description here

Simply put, the adapter mode mainly solves the problem of two incompatible interface connections.

How the Adapter Pattern Works

insert image description here

To put it simply, it is to convert incompatible classes into compatible classes to work together through adapters .

Three ways to implement the adapter pattern

  1. Class adapter : The adapter class inherits the adapted class by means of inheritance, so that the operation of the adapted class can be used and changed to achieve work compatibility .
  1. Object adapter: By aggregating the adapted class into the adapter class , and passing in the instance of the adapted class according to the construction method , the adapter class operates according to this instance , so as to achieve interface compatibility , which is recommended.
  1. Interface adapter: By defining an abstract class to inherit the interface , overwrite all the methods in the interface , so that subclasses inheriting this abstract class can selectively use the methods in the interface .

Hee hee, is it a little hard to understand? It doesn't matter. Let me demonstrate it, I believe everyone can understand.

Adapter small case

Now Xiaogao has a mobile phone (Phone), but unfortunately there is no electricity and needs to be charged, but the socket is 220V voltage, and the charging voltage of the mobile phone is 5V. At this time, it is necessary to design a conversion socket that converts the 220V voltage into 5V voltage. , can you help Xiao Gao design it?

Design with class adapters

This adapter improves the operation of the adapted class through inheritance , thereby achieving voltage compatibility.

Socket 220V class

package com.design_patterns.adapter.classadapter;

public class Voltage220V {
    
    
    //输出220伏电压
    public int output220V(){
    
    
        int src = 220;
        System.out.println("电压 = " + src + "伏");
        return src;
    }
}

An interface with an output voltage of 5V

package com.design_patterns.adapter.classadapter;

public interface IVoltage5V {
    
    
    public int output5V();
}

Class Adapter for Voltage Conversion

package com.design_patterns.adapter.classadapter;

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

    //覆写接口中的方法
    @Override
    public int output5V() {
    
    
        //获取到220V的电压
        int srcV = output220V();
        int dstV = srcV / 44;       //转成5V
        return dstV;
    }
}

mobile phone

package com.design_patterns.adapter.classadapter;

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,不可以充电~~~");
        }
    }
}

Client use and charge

package com.design_patterns.adapter.classadapter;

public class Client {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("====类适配器模式====");
        Phone phone = new Phone();
        phone.charging(new VoltageAdapter());
    }
}

operation result

====类适配器模式====
电压 = 220伏
电压为5V,可以充电~~~

Design with Object Adapters

This adapter improves the operation of the adapted class by means of aggregation , so as to achieve voltage compatibility. Only the differences from the above adapter are listed below .

Object adapter for voltage conversion

package com.design_patterns.adapter.objectadapter;

//适配器类
public class VoltageAdapter implements IVoltage5V {
    
    
    private Voltage220V voltage220V;        //关联关系-聚合

    //通过构造方法得到Voltage220V的对象实例
    public VoltageAdapter(Voltage220V voltage220V) {
    
    
        this.voltage220V = voltage220V;
    }

    /**
     * 将220V的电压转换为5V的电压的方法
     * 返回转换过后的电压
     * @return
     */
    @Override
    public int output5V() {
    
    
        //获取到220V的电压
        int srcV = this.voltage220V.output220V();
        int dstV = srcV / 44;
        System.out.println("使用对象适配器适配完成!!!");
        return dstV;
    }
}

Client use and charge

package com.design_patterns.adapter.objectadapter;

public class Client {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("====对象适配器模式====");
        Phone phone = new Phone();
        phone.charging(new VoltageAdapter(new Voltage220V()));
    }
}

operation result

====对象适配器模式====
电压 = 220伏
使用对象适配器适配完成!!!
电压为5V,可以充电~~~

Interface adapter, default adapter mode (custom small case)

defined interface

package com.design_patterns.adapter.interfaceadapter;

public interface Interface4 {
    
    
    public void m1();
    public void m2();
    public void m3();
    public void m4();
}

Defined abstract class

package com.design_patterns.adapter.interfaceadapter;

public abstract class AbsAdapter implements Interface4 {
    
    
    @Override
    public void m1() {
    
    

    }

    @Override
    public void m2() {
    
    

    }

    @Override
    public void m3() {
    
    

    }

    @Override
    public void m4() {
    
    

    }
}

Define the test class for testing

package com.design_patterns.adapter.interfaceadapter;

public class Client {
    
    
    public static void main(String[] args) {
    
    
        AbsAdapter absAdapter = new AbsAdapter() {
    
    
            /**
             * 此时只需要去覆盖我们需要使用的接口方法
             */
            @Override
            public void m1() {
    
    
                System.out.println("调用的是子类m1的方法");
            }
        };
        absAdapter.m1();
    }
}

operation result

调用的是子类m1的方法

Hee hee, although this case is very simple, it is still very useful in actual development. By designing an abstract class adapter to implement only the methods that need to be implemented.

Summarize

At this point, the basic content of the adapter has been sorted out. Hehe, maybe this time is not particularly vivid and interesting. It's good for friends to understand, thank you~

Guess you like

Origin blog.csdn.net/weixin_43479947/article/details/107539701