情景:美国的插座,提供110伏电压;中国的插座,提供220伏电压。编程
在中国,用两孔插座充电
而后坐飞机去美国旅游,假设美国某旅馆的墙上有只有一个三孔插座
幸亏我有美国适配器,一头插到三孔插座,另外一头转换成二孔插座,就能够给个人荣耀手机充电
在美国,经过美国适配器,用三空插座充电
框架
图:不一样国家的插座,插头不同,呵呵哒ide
图:因此须要写一个适配器模式测试
说明例子总共7个类
一个三孔插座接口(Adaptee, 被适配者)
一个三孔插座类
一个两孔插座接口(Target, 适配目标)
一个两孔插座类
一个适配器(Adapter:实现Target, 组合Adaptee)
一个手机类(Client)
一个Main类,用于测试this
package adapter; // adaptee(被适配者) ———— 假设在美国某旅馆的墙上,只有一个三孔插座
public interface ThreePinSoket { public void chargeWithThreePin(); public int voltage(); }
package adapter; // 实现一个具体的 adaptee
public class ThreePinSoketAmerica implements ThreePinSoket { @Override public void chargeWithThreePin() { System.out.println("美国标准的三孔的插座"); } @Override public int voltage() { return 110; // 美国电压是110伏
} }
package adapter; // client(具体的adaptee) ———— 这个就是我在中国的墙上的两个插孔的插座,我充电只能用这个
public class TwoPinSoketChina implements TwoPinSoket { @Override public void chargeWithTwoPin() { System.out.println("中国标准的两孔的插座"); } @Override public int voltage() { return 220; // 中国电压是220伏
} }
实现Target, 组合Adapteespa
package adapter; // 去美国旅游,必须带上一个“美国适配器”:实现两孔插座,组合三孔插座。用来给个人荣耀手机充电
public class AmericaAdapter implements TwoPinSoket // 实现两孔插座(target)
{ ThreePinSoket threePinSoket; // 组合三孔插座(adaptee)
public AmericaAdapter(ThreePinSoket threePinSoket) { this.threePinSoket = threePinSoket; } @Override public void chargeWithTwoPin() { threePinSoket.chargeWithThreePin(); } @Override public int voltage() { return threePinSoket.voltage() * 2; // 适配器把电压从 110V 升到 220V
} }
package adapter; public class RongYao { TwoPinSoket twoPinSoket; public RongYao() {} public void setTwoPinSoket(TwoPinSoket twoPinSoket) { this.twoPinSoket = twoPinSoket; } public void chargeRequest() { System.out.println("华为荣耀手机, " + twoPinSoket.voltage() + " 伏特充电中\n"); } }
package adapter; public class Main { public static void main(String[] args) { // 在中国,用两孔插座充电
TwoPinSoketChina twoPinSoketChina = new TwoPinSoketChina(); RongYao myRongYao = new RongYao(); myRongYao.setTwoPinSoket(twoPinSoketChina); myRongYao.chargeRequest(); // 而后坐飞机去美国旅游,美国某旅馆的墙上有只有一个三孔插座
ThreePinSoketAmerica threePinSoketAmerica = new ThreePinSoketAmerica(); testThreePin(threePinSoketAmerica); // 幸亏我有美国适配器,一头插到三孔插座,另外一头转换成二孔插座,就能够给个人荣耀手机充电
AmericaAdapter americaAdapter = new AmericaAdapter(threePinSoketAmerica); testTwoPin(americaAdapter); // 在美国,经过美国适配器,用三空插座充电
myRongYao.setTwoPinSoket(americaAdapter); myRongYao.chargeRequest(); } static void testTwoPin(TwoPinSoket twoPinSoket) { twoPinSoket.chargeWithTwoPin(); System.out.println("电压是" + twoPinSoket.voltage() + "伏特\n"); } static void testThreePin(ThreePinSoket threePinSoket) { threePinSoket.chargeWithThreePin(); System.out.println("电压是" + threePinSoket.voltage() + "伏特\n"); } }
华为荣耀手机,code
220 伏特充电中blog
美国标准的三孔的插座接口
电压是110伏特three
美国标准的三孔的插座
电压是220伏特
华为荣耀手机,
220 伏特充电中
适配器模式有三个重要角色:
这样,Adaptee和Target两个本来不兼容的接口,就能够在一块儿工做了(个人荣耀手机就能够在美国充电了)。这里的面向接口编程,获得了松耦合的效果。
美国的三孔插座能够实现Adaptee接口,那么英国、法国的三孔插座也能够去实现Adaptee接口,它们都成为了Adaptee接口的子类。在Adapter类中,因为组合了一个Adaptee的引用,根据Java的多态性,我就能够拿着相同的Adapter类去英国,法国充电了。
另外一方面,Client类组合一个Target接口的引用。咱们就可制造多个Adapter类,实现同一个Target接口。假设索尼手机的须要日本标准的两孔插座,那么写一个日本两孔插座类实现Target接口,我就能够拿着相同的Adapter类,在美国给日本的索尼手机充电了。