设计模式(8)の结构型の适配器模式(Adapter)
2018-03-02 17:44
471 查看
概述
适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。生活中的例子
例1:用电器做例子,笔记本电脑的插头一般都是三相的,即除了阳极、阴极外,还有一个地极。而有些地方的电源插座却只有两极,没有地极。电源插座与笔记本电脑的电源插头不匹配使得笔记本电脑无法使用。这时候一个三相到两相的转换器(适配器)就能解决此问题,而这正像是本模式所做的事情。
例2:
笔记本5V,插板儿220V,电脑电源适配器(”220V -> 5V”)
角色
术语目标(Target)角色:这就是所期待得到的接口。
源(Adapee)角色:现在需要适配的接口。
适配器(Adaper)角色:适配器类是本模式的核心。适配器把源接口转换成目标接口。显然,这一角色不可以是接口,而必须是具体类。
以笔记本电伏转换为分析对象:
Target:笔记本5V插口
Adapee:220V的插板儿
Adaper:笔记本电脑电源适配器(220V -> 5V)
俩种形式
适配器模式分为俩种形式:- 类适配器
- 对象适配器
他们的区别是Adaper的实现方式不一致,具体怎么个不一致,接下来我们在代码中来看,最后再在总结里进行阐述。
类适配器
上面我们说了俩种形式的区别在于Adapter的实现方式上面。那么类适配器(Adapter)的实现方式是怎样的呢?
结合我们的需求“电脑插电”,我直接贴出代码。
基本类
MacBook.classpackage adp.zj.com.adapterdesparttern.类适配器模式; /** * Created by thinkpad on 2018/3/2. */ public class MacBook { public boolean run(int power) { if (power != 5) { return false; } System.out.println("......Book is Running......"); return true; } }
Target
package adp.zj.com.adapterdesparttern.类适配器模式; /** * Created by thinkpad on 2018/3/2. */ public interface Target5V { int power_5v(); }
Adapee
package adp.zj.com.adapterdesparttern.类适配器模式; /** * Created by thinkpad on 2018/3/2. */ public class Adaptee220V { public int power_220v(){ return 220; } }
Adaper
package adp.zj.com.adapterdesparttern.类适配器模式; /** * Created by thinkpad on 2018/3/2. */ public class AdapterClass extends Adaptee220V implements Target5V { @Override public int power_5v() { return power_220v() / 44; } }
核心体现。extends Adapee220V implements Target5V
测试类
package adp.zj.com.adapterdesparttern.类适配器模式; /** * Created by thinkpad on 2018/3/2. */ public class ClassAdapterTest { public static void main(String[] args) { MacBook macBook = new MacBook(); AdapterClass adapter = new AdapterClass(); macBook.run(adapter.power_5v()); } }
对象适配器
对象型的跟上面的类适配器模式的区别在于,类适配型模式是继承extends的Adapee220V,而对象型的则不需要继承,而是持有它的实例。到这儿就是:
Adapter
package adp.zj.com.adapterdesparttern.对象适配器模式; import adp.zj.com.adapterdesparttern.类适配器模式.Adaptee220V; import adp.zj.com.adapterdesparttern.类适配器模式.Target5V; /** * Created by thinkpad on 2018/3/2. */ public class AdapterObject implements Target5V { Adaptee220V mAdapee220V = null; public AdapterObject(Adaptee220V mAdapee220V) { this.mAdapee220V = mAdapee220V; } @Override public int power_5v() { int i = 0; if (null != mAdapee220V) { i = mAdapee220V.power_220v(); } return i / 44; } public void setmAdapee220V(Adaptee220V mAdapee220V) { this.mAdapee220V = mAdapee220V; } }
测试类
package adp.zj.com.adapterdesparttern.对象适配器模式; import adp.zj.com.adapterdesparttern.类适配器模式.Adaptee220V; import adp.zj.com.adapterdesparttern.类适配器模式.MacBook; /** * Created by thinkpad on 2018/3/2. */ public class ObjectAdapterTest { public static void main(String[] args) { MacBook macBook = new MacBook(); AdapterObject adapter = new AdapterObject(new Adaptee220V()); macBook.run(adapter.power_5v()); } }
Demo
https://github.com/zj614android/AdapterDesignPatternThanks
三种适配器模式 总结和使用场景http://blog.csdn.net/zxt0601/article/details/52848004
《JAVA与模式》之适配器模式
http://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html
相关文章推荐
- 设计模式(五)适配器模式Adapter(结构型)
- 设计模式学习之适配器模式(Adapter,结构型模式)(14)
- 【设计模式学习笔记七】【结构型模式】【适配器模式(Adapter)】
- .Net设计模式 Adapter 适配器模式(结构型模式)Adapter Pattern
- 设计模式(七):Adapter 适配器模式 -- 结构型模式
- 结构型设计模式---适配器模式(adapter)
- 设计模式-结构型- 适配器模式(Adapter)
- "围观"设计模式(11)--结构型之适配器模式(Adapter Pattern)
- 设计模式 一 适配器模式 Adapter(结构型模式)
- 设计模式 (10)——适配器模式(Adapter Pattern,结构型)
- 设计模式(6) 结构型模式和适配器模式(ADAPTER)
- JAVA设计模式(6) —<结构型>适配器模式(Adapter)
- [设计模式笔记]二. 结构型模式--7.Adapter模式(适配器模式)(一)
- 23种设计模式(6)_结构型_适配器模式(Adapter Pattern)
- [设计模式笔记]二. 结构型模式--7.Adapter模式(适配器模式)(二)
- 设计模式06: Adapter 适配器模式(结构型模式)
- C#设计模式之六适配器模式(Adapter Pattern)【结构型】
- 设计模式--适配器模式Adapter(结构型)
- 设计模式深入学习---Adapter 适配器模式(结构型模式)
- 设计模式(六)适配器模式(Adapter)-结构型