java设计模式——适配器模式 Java源代码

前言:适配器模式就是把一个类的接口变换成客户端所能接受的另外一种接口,从而使两个接口不匹配而没法在一块儿工做的两个类可以在一块儿工做。一般被用在一个项目须要引用一些开源框架来一块儿工做时,这些框架的内部都有一些关于环境信息的接口,须要从外部引入,可是外部的接口不必定能匹配,在这种状况下,就须要适配器模式来转换接口。 

 

情景:美国的插座,提供110伏电压;中国的插座,提供220伏电压。编程

在中国,用两孔插座充电
而后坐飞机去美国旅游,假设美国某旅馆的墙上有只有一个三孔插座
幸亏我有美国适配器,一头插到三孔插座,另外一头转换成二孔插座,就能够给个人荣耀手机充电
在美国,经过美国适配器,用三空插座充电

框架

图:不一样国家的插座,插头不同,呵呵哒ide

适配器


图:因此须要写一个适配器模式测试

适配器类图

 

说明例子总共7个类 
一个三孔插座接口(Adaptee, 被适配者) 
一个三孔插座类 
一个两孔插座接口(Target, 适配目标) 
一个两孔插座类 
一个适配器(Adapter:实现Target, 组合Adaptee) 
一个手机类(Client) 
一个Main类,用于测试this

 


 

例子

三孔插座接口(Adaptee)

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伏 
 } }

 

两孔插座接口(Target)

package adapter; // client(具体的adaptee) ———— 这个就是我在中国的墙上的两个插孔的插座,我充电只能用这个
public class TwoPinSoketChina implements TwoPinSoket { @Override public void chargeWithTwoPin() { System.out.println("中国标准的两孔的插座"); } @Override public int voltage() { return 220;      // 中国电压是220伏
 } }

 

适配器(Adapter)

实现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 
 } }

 

手机类(Client)

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"); } }

 

Main类,用于测试

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 伏特充电中

 

分析

适配器模式有三个重要角色:

  • 目标角色(Target),要转换成的目标接口。在个人代码例子中,是中国的两孔接口
  • 源角色(Adaptee),须要被转换的源接口。在个人代码例子中,是美国的三孔接口
  • 适配器角色(Adapter),核心是实现Target接口, 组合Adaptee接口

这样,Adaptee和Target两个本来不兼容的接口,就能够在一块儿工做了(个人荣耀手机就能够在美国充电了)。这里的面向接口编程,获得了松耦合的效果。

美国的三孔插座能够实现Adaptee接口,那么英国、法国的三孔插座也能够去实现Adaptee接口,它们都成为了Adaptee接口的子类。在Adapter类中,因为组合了一个Adaptee的引用,根据Java的多态性,我就能够拿着相同的Adapter类去英国,法国充电了。

另外一方面,Client类组合一个Target接口的引用。咱们就可制造多个Adapter类,实现同一个Target接口。假设索尼手机的须要日本标准的两孔插座,那么写一个日本两孔插座类实现Target接口,我就能够拿着相同的Adapter类,在美国给日本的索尼手机充电了。

相关文章
相关标签/搜索