6.适配器模式

Posted zhihaospace

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了6.适配器模式相关的知识,希望对你有一定的参考价值。

一.介绍

技术图片

二.类适配器模式

1.介绍

技术图片

2.UML

技术图片

3.代码

Voltage220V.java

public class Voltage220V {

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

IVoltage5V.java

public interface IVoltage5V {

    int output5V();
}

VoltageAdapter.java

public class VoltageAdapter extends Voltage220V implements IVoltage5V {
    @Override
    public int output5V() {

        int srcV = output220V();
        int dstV = srcV / 44;
        return dstV;
    }
}

Phone.java

public class Phone {

    public void charging(IVoltage5V iVoltage5V){
        if (iVoltage5V.output5V() == 5){
            System.out.println("电压为5V,可以充电!");
        }else{
            System.out.println("电压超过5V,不可以充电!");
        }
    }
}

Client.java

public class Client {

    public static void main(String[] args) {
        Phone phone = new Phone();
        phone.charging(new VoltageAdapter());
    }
}

4.总结

技术图片

三.对象适配器模式

1.介绍

技术图片

 

 

2.UML

技术图片

3.代码

VoltageAdapter.java

public class VoltageAdapter extends Voltage220V implements IVoltage5V {

    private Voltage220V voltage220V;

    public VoltageAdapter(Voltage220V voltage220V) {
        this.voltage220V = voltage220V;
    }

    @Override
    public int output5V() {

        int dstV = 0;
        if (voltage220V!=null){
            int srcV = voltage220V.output220V();
            dstV = srcV / 44;
        }
        return dstV;
    }
}

Client.java

public class Client {

    public static void main(String[] args) {
        Phone phone = new Phone();
        phone.charging(new VoltageAdapter(new Voltage220V()));
    }
}

4.总结

技术图片

四.接口适配器模式

1.介绍

技术图片

2.UML

技术图片

3.代码

MyInterface.java

public interface MyInterface {

    void m1();
    void m2();
    void m3();
}

AdsAdapter.java

//抽象类
public abstract class AdsAdapter implements MyInterface {
    @Override
    public void m1() {

    }

    @Override
    public void m2() {

    }

    @Override
    public void m3() {

    }
}

Client.java

public class Client {
    public static void main(String[] args) {

        //匿名内部类实现抽象类的方法来初始化抽象类,相当于子类
        AdsAdapter adsAdapter = new AdsAdapter() {

            //只需要覆盖我们需要的接口方法即可
            @Override
            public void m2() {
                System.out.println("使用m2方法");
            }

        };

        adsAdapter.m2();
    }
}

4.总结

技术图片

五.模仿SpringMVC的适配器

1.UML

技术图片

2.代码

Controller.java

public interface Controller {
}

class SimpleController implements Controller {
    public void doSimpleHandler() {
        System.out.println("simple...");
    }
}

class HttpController implements Controller {
    public void doHttpHandler() {
        System.out.println("http...");
        }
        }

class AnnotationController implements Controller {
    public void doAnnotationHandler() {
        System.out.println("annotation...");
    }
}

HandlerAdapter.java

public interface HandlerAdapter {

    boolean supports(Object handler);

    void handle(Object handler);
}

class SimpleHandlerAdapter implements HandlerAdapter {

    @Override
    public boolean supports(Object handler) {
        return (handler instanceof SimpleController);
    }

    @Override
    public void handle(Object handler) {
        ((SimpleController) handler).doSimpleHandler();
    }
}


class HttpHandlerAdapter implements HandlerAdapter {

    @Override
    public boolean supports(Object handler) {
        return (handler instanceof HttpController);
    }

    @Override
    public void handle(Object handler) {
        ((HttpController) handler).doHttpHandler();
    }
}

class AnnotationHandlerAdapter implements HandlerAdapter {

    @Override
    public boolean supports(Object handler) {
        return (handler instanceof AnnotationController);
    }

    @Override
    public void handle(Object handler) {
        ((AnnotationController) handler).doAnnotationHandler();
    }
}

DispatchServlet.java

public class DispatchServlet {
    public static List<HandlerAdapter> handlerAdapterList = new ArrayList<>();

    public DispatchServlet(){
        handlerAdapterList.add(new SimpleHandlerAdapter());
        handlerAdapterList.add(new HttpHandlerAdapter());
        handlerAdapterList.add(new AnnotationHandlerAdapter());
    }

    public void doDispatch(Controller controller){

        HandlerAdapter adapter = getHandler(controller);
        if (adapter != null){
            adapter.handle(controller);
        }else {
            System.out.println("没有合适的HandlerAdapter去处理");
        }
    }

    public HandlerAdapter getHandler(Controller controller){

        for (HandlerAdapter adapter : this.handlerAdapterList) {
            if (adapter.supports(controller)){
                return adapter;
            }
        }
        return null;
    }
}

Client.java

public class Client {
    public static void main(String[] args) {

        Controller controller1 = new HttpController();
        Controller controller2 = new SimpleController();
        Controller controller3 = new AnnotationController();
        new DispatchServlet().doDispatch(controller1);
        new DispatchServlet().doDispatch(controller2);
        new DispatchServlet().doDispatch(controller3);
    }
}

 

 

以上是关于6.适配器模式的主要内容,如果未能解决你的问题,请参考以下文章

6.适配器设计模式

Java读源代码学设计模式:适配器Adapter

如何使用 Android 片段?

如何将数据从回收器适配器发送到片段 |如何从 recyclerview 适配器调用片段函数

如何从片段适配器启动活动

设计模式学习笔记------适配器模式