适配器模式Adapter

  • 前言
  • 示例说明
  • 源码下载
  • 类适配器模式
    • UML
    • 示例代码
    • 测试结果
  • 对象适配器模式
    • UML
    • 示例代码
    • 测试结果

前言

Convert the interface of a class into another interface clients expect.Adapter lets classes work together that couldn’t otherwise
because of incompatible interfaces.

将一个类的接口变成客户端所期待的另一个接口,从而使原本因接口不匹配而无法一起工作的两个类能够一起工作。


示例说明

通过球员的进攻防守说明,美国的前锋、中锋、后卫的进攻attack()、防守guard()用英文输出
中国球员的进攻防守进攻用进攻()、防守用防守()用英文输出 (起中文函数名实属无奈,只是为了更好的区分,有强迫症的请忽略…)

然后在客户端测试时,不论中国球员还是美国球员都是进攻调用attack()方法、防守调用guard()方法


源码下载

GitHub:https://github.com/YEN-GitHub/PatternDemo/tree/master/src/StructuralPattern/AdapterPattern


类适配器模式

UML

适配器模式Adapter_第1张图片

示例代码

package StructuralPattern.AdapterPattern.demo2;

/**
 * Created by yangenneng on 2017-05-10 21:51
 * Abstract:
 */
public interface AmericanPlayer {
    //进攻
    public void attack();

    //防守
    public void guard();
}
package StructuralPattern.AdapterPattern.demo2;


import StructuralPattern.AdapterPattern.demo2.AmericanPlayer;

/**
 * Created by yangenneng on 2017-05-10 21:32
 * Abstract:前锋
 */
public class Forward implements AmericanPlayer {

    private String name;

    public Forward(String name){this.name=name;}

    //进攻
    public void attack(){
        System.out.println("American Player Forward:"+name+" attack");
    }

    //防守
    public void guard(){
        System.out.println("American Player Forward:"+name+" guard");
    }


}
package StructuralPattern.AdapterPattern.demo2;


/**
 * Created by yangenneng on 2017-05-10 21:35
 * Abstract:中锋
 */
public class Centre implements AmericanPlayer {

    private String name;

    public Centre(String name){this.name=name;}

    //进攻
    public void attack(){
        System.out.println("American Player Centre:"+name+" attack");
    }

    //防守
    public void guard(){
        System.out.println("American Player Centre:"+name+" guard");
    }

}
package StructuralPattern.AdapterPattern.demo2;

import StructuralPattern.AdapterPattern.demo2.AmericanPlayer;

/**
 * Created by yangenneng on 2017-05-10 21:37
 * Abstract:后卫
 */
public class Guard implements AmericanPlayer {

    private String name;

    public Guard(String name){this.name=name;}

    //进攻
    public void attack(){
        System.out.println("American Player Guard:"+name+" attack");
    }

    //防守
    public void guard(){
        System.out.println("American Player Guard:"+name+" guard");
    }

}
package StructuralPattern.AdapterPattern.demo2;

/**
 * Created by yangenneng on 2017-05-10 21:37
 * Abstract:中国球员
 */
public class ChinaPlayer {
    protected String name;

    public ChinaPlayer(String name){this.name=name;}

    //进攻
    public void 进攻(){
        System.out.println("中国球员:"+name+"进攻");
    }

    //防守
    public void 防守(){
        System.out.println("中国球员:"+name+"防守");
    }
}
package StructuralPattern.AdapterPattern.demo2;

/**
 * Created by yangenneng on 2017-05-10 21:56
 * Abstract:
 */
public class Adapter extends ChinaPlayer implements AmericanPlayer{

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


    @Override
    public void attack() {
        super.进攻();
    }

    @Override
    public void guard() {
        super.防守();
    }
}
package StructuralPattern.AdapterPattern.demo2;

/**
 * Created by yangenneng on 2017-05-10 21:59
 * Abstract:客户端测试
 */
public class Client {
    public static void main(String[] args) {
        System.out.println("类适配器模式");

        AmericanPlayer americanPlayer1=new Forward("aaa");
        AmericanPlayer americanPlayer2=new Centre("bbb");
        AmericanPlayer americanPlayer3=new Guard("ccc");

        Adapter chinaPlayer1=new Adapter("姚明");


        //进攻  都是调用attack方法
        americanPlayer1.attack();
        americanPlayer2.attack();
        americanPlayer3.attack();
        chinaPlayer1.attack();

        System.out.println();

        //防守  都是调用guard方法
        americanPlayer1.guard();
        americanPlayer2.guard();
        americanPlayer3.guard();
        chinaPlayer1.guard();
    }
}

测试结果

适配器模式Adapter_第2张图片

对象适配器模式

UML

适配器模式Adapter_第3张图片

示例代码

package StructuralPattern.AdapterPattern.demo1;

/**
 * Created by yangenneng on 2017-05-10 21:30
 * Abstract:美国球员
 */
public class AmericanPlayer {
    protected String name;

    public AmericanPlayer(String name){this.name=name;}

    //进攻
    public void attack(){

    }

    //防守
    public void guard(){

    }

}
package StructuralPattern.AdapterPattern.demo1;


/**
 * Created by yangenneng on 2017-05-10 21:32
 * Abstract:前锋
 */
public class Forward  extends AmericanPlayer{

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

    //进攻
    public void attack(){
        System.out.println("American Player Forward:"+name+" attack");
    }

    //防守
    public void guard(){
        System.out.println("American Player Forward:"+name+" guard");
    }


}
package StructuralPattern.AdapterPattern.demo1;

/**
 * Created by yangenneng on 2017-05-10 21:35
 * Abstract:中锋
 */
public class Centre extends AmericanPlayer {

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

    //进攻
    public void attack(){
        System.out.println("American Player Centre:"+name+" attack");
    }

    //防守
    public void guard(){
        System.out.println("American Player Centre:"+name+" guard");
    }

}
package StructuralPattern.AdapterPattern.demo1;

/**
 * Created by yangenneng on 2017-05-10 21:37
 * Abstract:后卫
 */
public class Guard extends AmericanPlayer {

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

    //进攻
    public void attack(){
        System.out.println("American Player Guard:"+name+" attack");
    }

    //防守
    public void guard(){
        System.out.println("American Player Guard:"+name+" guard");
    }

}
package StructuralPattern.AdapterPattern.demo1;

/**
 * Created by yangenneng on 2017-05-10 21:37
 * Abstract:中国球员
 */
public class ChinaPlayer {
    protected String name;

    public ChinaPlayer(String name){this.name=name;}

    //进攻
    public void 进攻(){
        System.out.println("中国球员:"+name+"进攻");
    }

    //防守
    public void 防守(){
        System.out.println("中国球员:"+name+"防守");
    }
}
package StructuralPattern.AdapterPattern.demo1;

import StructuralPattern.AdapterPattern.demo1.*;

/**
 * Created by yangenneng on 2017-05-10 21:42
 * Abstract:
 */
public class Adapter extends AmericanPlayer {

    private ChinaPlayer chinaPlayer=null;

    public Adapter(String name){
        super(name);
        chinaPlayer=new ChinaPlayer(name);
    }

    //进攻
    public void attack(){
        chinaPlayer.进攻();
    }

    //防守
    public void guard(){
        chinaPlayer.防守();
    }
}
package StructuralPattern.AdapterPattern.demo1;

/**
 * Created by yangenneng on 2017-05-10 21:44
 * Abstract:客户端测试代码
 */
public class Client {
    public static void main(String[] args) {
        System.out.println("对象适配器模式");

        AmericanPlayer americanPlayer1=new Forward("aaa");
        AmericanPlayer americanPlayer2=new Centre("bbb");
        AmericanPlayer americanPlayer3=new Guard("ccc");

        Adapter chinaPlayer1=new Adapter("姚明");

        //进攻  都是调用attack方法
        americanPlayer1.attack();
        americanPlayer2.attack();
        americanPlayer3.attack();
        chinaPlayer1.attack();

        System.out.println();

        //防守  都是调用guard方法
        americanPlayer1.guard();
        americanPlayer2.guard();
        americanPlayer3.guard();
        chinaPlayer1.guard();

    }
}

测试结果

适配器模式Adapter_第4张图片

你可能感兴趣的:(----【设计模式】,编程体系相关基础,源码,对象,适配器,适配器模式,adapter)