23中设计模式之适配器模式

x33g5p2x  于2022-03-20 转载在 其他  
字(3.2k)|赞(0)|评价(0)|浏览(269)

适配器模式

适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,适配器模式将一个类的接口适配成用户所期待的。共有两类适配器模式:

  • 类适配器模式
    这种适配器模式下,适配器继承自已实现的类(一般多重继承)
  • 对象适配器模式
    在这种适配器模式中,适配器容纳一个它包裹的类的实例。在这种情况下,适配器调用被包裹对象的物理实体

类适配器

适配器模式包含以下主要角色:

  • 目标接口:当前系统业务所期待的接口,它可以使抽象类或接口
  • 被适配器类:它是被访问和适配的现存组件库中的组件接口
  • 适配者类:它是转化器,通过继承或引用被适配者的对象,把被适配者接口换成目标接口,让客户按照目标接口的格式访问被适配者

【案例】

以生活中的充电器为例,充电器本身相当于适配器Adapter,220V交流电相当于被适配者,目标接口就是5V直流电

public class ClassAdapterTest { //客户端
    public static void main(String[] args) {
        Phone phone = new Phone();
        VoltageAdapter adapter = new VoltageAdapter();
        phone.charging(adapter);
    }
}
class Volatge220V{ //被适配者类
    public int output220V(){
        int src = 220;
        System.out.println("电压="+src+"伏");
        return src;
    }
}
interface IVoltage5V{ //目标接口
    int output5V();
}

class VoltageAdapter extends Volatge220V implements IVoltage5V{ //适配者类
    public int output5V() {
        int srcV = output220V();
        System.out.println("适配器做适配处理");
        int dstV = srcV/44;
        return dstV ;
    }
}
class Phone{
    public void charging(IVoltage5V iVoltage5V){
        if (iVoltage5V.output5V() == 5){
            System.out.println("电压为5V,可以充电");
        }else{
            System.out.println("电压不为5V,不可以充电");
        }
    }
}

对象适配器

对象适配器的基本思路和类适配器相同,只是将Adapter类做修改,Adapter不再继承被适配者类,而是持有被适配者类的实例,已解决兼容的问题

public class ObjectAdapterTest {//客户端
    public static void main(String[] args) {
        Phone phone = new Phone();
        phone.charging(new VoltageAdapter(new Volatge220V()));
    }
}
class Volatge220V{ //被适配者类
    public int output220V(){
        int src = 220;
        System.out.println("电压="+src+"伏");
        return src;
    }
}
interface IVoltage5V{ //目标接口
    int output5V();
}
class VoltageAdapter implements IVoltage5V{ //适配者类
    private Volatge220V volatge220V;

    public VoltageAdapter(Volatge220V volatge220V) {
        this.volatge220V = volatge220V;
    }

    public int output5V() {
        int dstV = 0;
        if (volatge220V != null) {
            int srcV = volatge220V.output220V();
            System.out.println("适配器做适配处理");
            dstV = srcV/44;
        }
        return dstV ;
    }
}
class Phone{
    public void charging(IVoltage5V iVoltage5V){
        if (iVoltage5V.output5V() == 5){
            System.out.println("电压为5V,可以充电");
        }else{
            System.out.println("电压不为5V,不可以充电");
        }
    }
}

接口适配器模式

接口适配器模式也叫缺省适配器模式,当不需要全部实现接口所提供的方法时,可先设计一个抽象类实现接口,并为该接口中的每个方法提供一个默认的实现(空方法),那么该抽象类的子类可以有选择的覆盖父类中的某些方法实现需求。适用于一个接口不想使用其他方法的情况

public class InterfaceAdapterTest {
    public static void main(String[] args) {
        AbsAdapter adapter = new AbsAdapter() {
            //只需要去覆盖我们需要的接口方法即可
            @Override
            public void m1() {
                System.out.println("使用了m1方法");
            }
        };

        adapter.m1();
    }
}

interface MyInterface{
    void m1();
    void m2();
    void m3();
    void m4();
    void m5();
}
abstract class AbsAdapter implements MyInterface{
    public void m1() {}//默认实现
    public void m2() {}//默认实现
    public void m3() {}//默认实现
    public void m4() {}//默认实现
    public void m5() {}//默认实现
}

Servlet就使用了缺省适配器模式,GenericServlet就是其中的适配者类

public interface Servlet {
    void init(ServletConfig var1) throws ServletException;
    void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
    void destroy();
}
public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {
	//实现了Servlet接口中的init()和desotry()方法
	public void init(ServletConfig config) throws ServletException {
        this.config = config;
        this.init();
    }
    public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
	public void destroy() {}
}
public abstract class HttpServlet extends GenericServlet {
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        long lastModified;
        //下面的代码省略了...          
    }
}

相关文章

微信公众号

最新文章

更多