适配器模式

作者: MC_Honva | 来源:发表于2018-12-14 17:18 被阅读2次

适配器模式分为类适配器和对象适配器

adapter.png
一、对象适配器

概述:采用对象组合方式实现,在适配器标准接口中创建需要适配的类对象,持有该对象,在构造函数中传入此对象,后期调用

/**
 * 指定适配类
 * @author Ticker
 *
 */
public class Adaptee {
    public void specificeReques(){
        System.out.println("特殊适配");
    }
}
/**
 * 要求接口
 * @author Ticker
 *
 */
public interface Target {
    void requst();
}
/**
 * 目标适配器
 * @author Ticker
 *
 */
public class Adapter implements Target{
    private Adaptee adaptee;
    
    public Adapter(Adaptee adaptee) {
        super();
        this.adaptee = adaptee;
    }

    @Override
    public void requst() {
        adaptee.specificeReques();
    }

}

public class Main {

    /** 主类调用
     * @param args
     */
    public static void main(String[] args) {
        Adapter adapter = new Adapter(new Adaptee());
        adapter.requst();

    }

}

二、类适配器(采用继承模式)

概述:继承adaptee类,实现目标接口,相对于对象适配,类的适配更加简单

/**
 * 目标适配器
 * @author Ticker
 *
 */
public class Adapter extends Adaptee implements Target{

    @Override
    public void requst() {
        specificeReques();
    }

}
优点:

1、将目标类和适配者类解耦
2、通过引入一个适配器类重用现有的适配者类,而无需修改原有代码,更好的扩展性

相关文章

  • Java设计模式(二)

    talk is cheap show me the code 适配器模式 类适配器模式 接口适配器模式 对象适配器...

  • 适配器模式

    目录 1、什么是适配器模式? 2、适配器模式结构? 3、如何实现适配器模式? 4、适配器模式的特点? 5、适配器模...

  • 设计模式之适配器模式

    适配器模式: 类适配器模式、对象适配器模式、接口适配器模式 1.类适配器模式:新的接口出现了,但是和老的接口不兼容...

  • 学习iOS设计模式第一章 适配器(Adapter)

    今天学习了iOS设计模式中的适配器模式,适配器有两种模式对象适配器模式-- 在这种适配器模式中,适配器容纳一个它包...

  • 第4章 结构型模式-适配器模式

    一、适配器模式简介 二、适配器模式的优点 三、适配器模式的实例

  • 设计模式(Design Patterns)适配器模式(Adapt

    适配器模式主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。 类的适配器模式 场景:将一个类转换成...

  • 适配器模式

    适配器模式主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。适配器模式将某个类的接口转换成客户端期...

  • 适配器模式

    先直观感受下什么叫适配器 适配器模式有类的适配器模式和对象的适配器模式两种不同的形式。 类适配器模式 对象适配器模...

  • 适配器模式

    适配器模式 一、适配器模式定义 适配器模式的定义是,Convert the interface of a clas...

  • 设计模式:结构型

    享元模式 (Pools,Message) 代理模式 适配器模式 :类适配器和对象适配器 装饰者模式 外观模式 桥接...

网友评论

    本文标题:适配器模式

    本文链接:https://www.haomeiwen.com/subject/aggghqtx.html