Lying design pattern notes (xiv) の adapter mode

For chestnuts

Problem Description

People in different countries play in the NBA, but they are to communicate in English.

Simple implementation

Player

/**
 * 球员
 * Created by callmeDevil on 2019/8/4.
 */
public abstract class Player {

    protected String name;

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

    // 发起进攻
    public abstract void attack();
    // 转回防守
    public abstract void defense();

}

Forwards

/**
 * 前锋
 * Created by callmeDevil on 2019/8/4.
 */
public class Forwards extends Player {

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

    @Override
    public void attack() {
        System.out.println(String.format("前锋 %s 进攻", name));
    }

    @Override
    public void defense() {
        System.out.println(String.format("前锋 %s 防守", name));
    }
}

Center

/**
 * 中锋
 * Created by callmeDevil on 2019/8/4.
 */
public class Center extends Player {
    // 代码与前锋相似
}

Guards

/**
 * 后卫
 * Created by callmeDevil on 2019/8/4.
 */
public class Guards extends Player {
    // 代码与前锋相似
}

test

public class Test {
    public static void main(String[] args) {
        Player b = new Forwards("巴蒂尔");
        b.attack();
        Player m = new Guards("麦克格雷迪");
        m.attack();

        Player ym = new Center("姚明");
        // 姚明问:attack 和 defense 是什么意思?
        ym.attack();
        ym.defense();
    }
}

Test Results

前锋 巴蒂尔 进攻
后卫 麦克格雷迪 进攻
中锋 姚明 进攻
中锋 姚明 防守

There is a problem

When Yao Minggang the NBA might English is not very good, that is, listen to understand the coach's tactical arrangements, attach and defense do not know what it meant, so this realization there will be problems, you need a translation in English.

Adapter mode

definition

Converting the interface of a class clients expect another interface. Adapter pattern makes those classes otherwise because of incompatible interfaces can not work together to work together.

classification

It is divided into classes adapter mode and object adapter mode . Since the class Adapter pattern through multiple inheritance to one interface to another interface match, Java and other language does not support multiple inheritance , which is a class has only one parent, so here is primarily concerned with the object adapter .

UML diagrams

Code

ForeignCenter

/**
 * 外籍中锋
 * Created by callmeDevil on 2019/8/4.
 */
public class ForeignCenter {

    // 外籍中锋球员的姓名故意用属性而不是构造方法来区别与三个球员的不同
    private String name;

    // 表明外籍中锋只懂中文“进攻”(注意:举例效果,实际上千万别用这种方式编程)
    public void 进攻(){
        System.out.println(String.format("外籍中锋 %s 进攻", name));
    }

    // 表明外籍中锋只懂中文“防守”(注意:举例效果,实际上千万别用这种方式编程)
    public void 防守(){
        System.out.println(String.format("外籍中锋 %s 防守", name));
    }

    public String getName() {
        return name;
    }

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

Translator

/**
 * 翻译者
 * Created by callmeDevil on 2019/8/4.
 */
public class Translator extends Player{

    // 声明并实例化一个内部“外籍中锋”对象,表明翻译者与外籍球员有关联
    private ForeignCenter wjzf = new ForeignCenter();

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

    @Override
    public void attack() {
        // 翻译者将“attack”翻译为“进攻”告诉外籍中锋
        wjzf.进攻();
    }

    @Override
    public void defense() {
        // 翻译者将“defense”翻译为“防守”告诉外籍中锋
        wjzf.防守();
    }

}

test

public class Test {
    public static void main(String[] args) {
        Player b = new Forwards("巴蒂尔");
        b.attack();
        Player m = new Guards("麦克格雷迪");
        m.attack();

        Player ym = new Translator("姚明");
        // 翻译者告诉姚明,教练要求你既要“进攻”又要“防守”
        ym.attack();
        ym.defense();
    }
}

Test Results

前锋 巴蒂尔 进攻
后卫 麦克格雷迪 进攻
外籍中锋 姚明 进攻
外籍中锋 姚明 防守

to sum up

  • Data and behavior of the system are correct, but does not match the interface, we should consider the use of adapters, the purpose is to make a original objects outside the control of an interface to match.
  • Adapter mode is mainly used want to reuse some of the existing classes, but the interface and inconsistent with the multiplex environment.
  • The use of existing classes, but if it's the interface, that is, its methods and your request is not the same, you should consider using an adapter mode.
  • Two things done the same or similar type but having a different interface to use it.
  • Unified client code can call the same interface on the line, so you can more simple, straightforward and compact.
  • In both not easy to modify the time and then use the adapter mode adaptation.

Guess you like

Origin www.cnblogs.com/call-me-devil/p/11298653.html