适配器模式

作者: fancy_boy_石嘉成 | 来源:发表于2018-07-29 17:24 被阅读6次

《大话设计模式》阅读笔记和总结。原书是C#编写的,本人用Java实现了一遍,包括每种设计模式的UML图实现和示例代码实现。
目录:设计模式
Github地址:DesignPattern

说明

定义:适配器模式(Adapter),将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

UML图:

适配器模式UML图.png

代码实现:

Target,这是客户所期待的接口。目标可以是具体的或者抽象的类,也可以是接口

abstract class Target{
    public void Request(){
        System.out.println("普通请求");
    }
}

adptee 需要适配的类

class Adaptee{
    public void SpecificRequest() {
        System.out.println("特殊请求");
    }
}

Adapter通过在内部包装一个Adaptee对象,把原接口转换为目标接口

class Adapter extends Target{
    private Adaptee adaptee = new Adaptee();
    
    @Override
    public void Request() {
        adaptee.SpecificRequest();
    }
}

客户端代码

public class AdapterDesign {
    
    public static void main(String[] args) {
        Target target = new Adapter();
        target.Request();
    }

}

运行结果

特殊请求

示例

例子:在NBA火箭队比赛中,教练给每个球员布置进攻和防守的任务,姚明作为外籍球员需要一个翻译。用代码模拟这个过程。

UML图:

适配器模式示例UML图.png

代码实现:

球员

public abstract class Player {

    protected String name;
    public Player(String name){
        this.name = name;
    }
    
    // 进攻
    public abstract void Attack();
    // 防守
    public abstract void Defense();
}

前锋,中锋,后卫代码类似

public class Forwards extends Player{

    public Forwards(String name) {
        super(name);
    }

    @Override
    public void Attack() {
        System.out.println(name+"进攻");
        
    }

    @Override
    public void Defense() {
        System.out.println(name+"防守");
        
    }
}

外籍中锋

public class ForeignCenter{

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * 外籍中锋表示只能听懂汉语拼音jingong
     */
    public void jingong() {
        System.out.println("外籍中锋"+name+"进攻");
        
    }
    /**
     * 外籍中锋表示只能听懂汉语拼音fangshou
     */
    public void fangshou() {
        System.out.println("外籍中锋"+name+"防守");
        
    }
}

翻译

public class Translator extends Player{
    
    private ForeignCenter foreignCenter = new ForeignCenter();

    public Translator(String name) {
        super(name);
        foreignCenter.setName(name);
    }

    @Override
    public void Attack() {
        foreignCenter.jingong();
        
    }

    @Override
    public void Defense() {
        foreignCenter.fangshou();
    }

}

客户端代码

public class Main {
    public static void main(String[] args) {
        Player maidi = new Forwards("麦迪");
        maidi.Attack();
        maidi.Defense();
        
        Player badier = new Forwards("巴蒂尔");
        badier.Defense();
        
        // 翻译告诉姚明,教练要你进攻和防守
        Player yaoming = new Translator("姚明");
        yaoming.Attack();
        yaoming.Defense();
    }

}

运行结果

麦迪进攻
麦迪防守
巴蒂尔防守
外籍中锋姚明进攻
外籍中锋姚明防守

相关文章

  • 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/ryqtvftx.html