/* * 此例为英标插头提供能够正常插入国标插座的转换器。不过省去调节电压等细 * 节。 */ // Target - 目标抽象类(或接口) public class ChinaSocket { public void workInAWay() { System.out.println("You are using ChinaScoket"); } }
// Adaptee - 适配者类 public class EnglandSocket { public void workInBWay() { System.out.println("You are using EnglandSocket"); } }
// Adapter - 适配器类 public class ChinaScoketAdapter { EnglandScoket englandScoket; public ChinaScoketAdapter(EnglandScoket englandScoket) { this.englandScoket = englandScoket; } public void workInAWay() { englandScoket.workInBWay(); } }
// Client - 客户端 public class Test { public static void main(String[] args) { ChinaSocket chinaSocket = new ChinaSocket(); EnglandSocket englandSocket = new EnglandSocket(); ChinaScoketAdapter adapter = new ChinaScoketAdapter(englandSocket); chinaSocket.workInAWay(); adapter.workInAWay(); } }