Design Patterns - Adapter
# 适配器(Adapter)
# Intent
把一个类接口转换成另一个用户需要的接口。
# Class Diagram
# Implementation
鸭子(Duck)和火鸡(Turkey)拥有不同的叫声,Duck 的叫声调用 quack () 方法,而 Turkey 调用 gobble () 方法。
要求将 Turkey 的 gobble () 方法适配成 Duck 的 quack () 方法,从而让火鸡冒充鸭子!
public interface Duck {
void quack();
}
public interface Turkey {
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 Client {
public static void main(String[] args) {
Turkey turkey = new WildTurkey();
Duck duck = new TurkeyAdapter(turkey);
duck.quack();
}
}
# JDK
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment
DisqusGitalk