适配器(Adapter)模式
2019-08-02 本文已影响0人
莫库施勒
将一个类的接口,转换为客户期望的另一个接口
public interface Duck {
public void quack();
}
public interface Turkey {
public void gobble();
}
public class WildTurkey implements Turkey{
@Override
public void gobble() {
System.out.println("gobble!");
}
}
public class TurkeyAdapter implements Duck{
Turkey turkey;
public TurkeyAdapter(Turkey turkey) {
this.turkey = turkey;
}
@Override
public void quack() {
turkey.gobble();
}
}
public class DuckTestDrive {
public static void main(String[] args) {
Turkey turkey = new WildTurkey();
Duck duck = new TurkeyAdapter(turkey);
duck.quack();
}
}