将一个类的接口,转换为客户期望的另一个接口
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();
}
}
网友评论