被适配的类:
public class Adaptee {
public void specificMethod(){
System.out.println("三脚插头");
}
}
适配接口
public interface AppleInterface {
public void phoneInterface();
}
适配器
public class Adapter implements AppleInterface {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void phoneInterface() {
adaptee.specificMethod();
}
}
参照类
public class Linghtning implements AppleInterface {
@Override
public void phoneInterface() {
System.out.println("Linghtning.");
}
}
调用者
public class Consumer {
public static void main(String[] args) {
// 适配到三脚插头
AppleInterface linghtning = new Adapter(new Adaptee());
linghtning.phoneInterface();
// 只是自己
linghtning = new Linghtning();
linghtning.phoneInterface();
}
}