结构型设计模式之适配器模式

适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。将一个类的接口转换成客户希望的另外一个接口。使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

适配器模式在SpringMVC框架中的应用

SpringMVC中的 HandlerAdapter ,使用了适配器模式
使用HandlerAdapter的原因分析
处理器的类型不同,有多种实现方式,其调用方式是不确定的。
如果需要直接调用Controller方法,需要调用的时候,就不得不使用if条件判断是哪一种子类后再执行
如果后面要拓展Controller,就得修改原来的代码,违背了OCP开闭原则(Open Closed Principle)
springmvc5中有四个非抽象的HandlerAdapter:
1.SimpleServletHandlerAdapter
2.SimpleControllerHandlerAdapter
3.HttpRequestHandlerAdapter
4.RequestMappingHandlerAdapter
springmvc通过HandlerMapping将请求URL映射到handler,再将handler交给HandlerAdapter进行调用处理。

1.类适配器模式

在需要不改变原有接口或类结构的情况下扩展类的功能以适配不同的接口时,可以使用类适配器模式。通过继承一个原有类(需要拓展的类)并实现新接口的适配器实现。

1.1 定义Source类

/**
 * 定义Source类
 */
public class Source {
    public void editText(){
        System.out.println("edit text...");
    }
}

1.2 定义Targetable接口

/**
 * 定义Targetable接口
 */
public interface Targetable {
    void editText();
    void editWord();
}

1.3 定义Adapter继承Source类并实现Targetable接口

/**
 * 定义Adapter继承Source类并实现Targetable接口
 * 类适配器通过继承一个原有类(需要拓展的类)并实现新接口的适配器实现
 */
public class ClassAdapter extends Source implements Targetable {
    @Override
    public void editWord() {
        System.out.println("edit word...");
    }
}

1.4 使用类适配器

/**
 * 使用类适配器
 */
public class ClassAdapterDemo {
    public static void main(String[] args) {
        Targetable targetable = new ClassAdapter();
        targetable.editText();
        targetable.editWord();
    }
}

2. 对象适配器

对象适配器的思路和类适配器基本相同,只是修改了Adapter类,Adapter不再继承Source类,而是持有Source类的实例,以解决兼容性问题

2.1 对象适配器的定义如下:

import adapter.class_adapter.Source;
import adapter.class_adapter.Targetable;

public class ObjectAdapter implements Targetable {
    private Source source;
    public ObjectAdapter(Source source){
        super();
        this.source=source;
    }

    @Override
    public void editText() {
        this.source.editText();
    }

    @Override
    public void editWord() {
        System.out.println("edit word...");
    }
}

2.2 对象适配器的使用

/**
 * 使用对象适配器
 */
public class ObjectAdapterDemo {
    public static void main(String[] args) {
        Source src = new Source();
        Targetable targetable = new ObjectAdapter(src);
        targetable.editWord();
        targetable.editText();
    }
}

3. 接口适配器

在不希望实现一个接口中的所有方法时,可以创建一个抽象类实现接口中的所有方法,在使用时继承这个抽象类按需实现其中的方法即可

具体代码实现发下:

3.1 定义公共接口Sourceable

/**
 * 定义Sourceable接口,定义生产手机和电脑的方法
 */
public interface Sourceable {
    void createPhone();
    void createComputer();
}

3.2 定义抽象类,实现公共接口的方法,不做具体实现

/**
 * 定义Sourceable的抽象实现类AbstractAdapter,该类对Sourceable接口的方法进行重写,不做具体实现
 */
public class AbstractAdapter implements Sourceable {
    @Override
    public void createPhone() {

    }

    @Override
    public void createComputer() {

    }
}

3.3 定义SourceSub1类,按需求实现createPhone()方法,定义SourceSub2类,按需求实现createPhone()方法

public class SourceSub1 extends AbstractAdapter {
    @Override
    public void createPhone() {
        System.out.println("create phone...");
    }
}

public class SourceSub2 extends AbstractAdapter{
    @Override
    public void createComputer() {
        System.out.println("create computer...");
    }
}

3.4 接口适配的使用

/**
 * 使用接口适配器
 */
public class AbstaractAdapterDemo {
    public static void main(String[] args) {
        Sourceable s1= new SourceSub1();
        Sourceable s2= new SourceSub2();
        s1.createPhone();
        s2.createComputer();
    }
}

 

 

 
posted @ 2021-03-06 20:10  黄忠  阅读(102)  评论(0编辑  收藏  举报