设计模式【适配器模式】
2014-10-24 23:28
141 查看
适配器模式的核心思想:把原有的接口转换成调用者所期望的接口,从而使不同接口的类可以一起工作。
在此,需要介绍适配器中包含的3个角色:
■源角色Adaptee:需要适配的目标或接口。
■目标角色Target:所期望得到的接口。
■适配器角色Adapter:适配类是本模式的核心,用来把源接口转换成目标接口,显然这三者的相互关系组成了适配器模式的原型。
【转载使用,请注明出处:http://blog.csdn.net/mahoking】
适配器模式也叫做包装器模式(Wrapper),根据适配器对象的不同,又可以将适配器模式为3种子类。
1、 类的适配器模式:用于对类的进行适配。
2、 对象的适配器模式:用于对对象进行包装。
3、 接口的适配器模式:用于对接口的抽象化。
第一种:类的适配群器模式。
第二种:对象的适配器模式。
第三种:接口的适配器模式。
Sourceable是定义了多个接口函数的接口类。
DefaultAdapter是一个抽象类,它实现了接口Sourceable,并且为每个接口函数提供默认的实现。依据DefaultAdapter就可以编写不同的实现,在实现中只需要重写部分待实现的函数,而并非要重写全部。
【转载使用,请注明出处:http://blog.csdn.net/mahoking】
在此,需要介绍适配器中包含的3个角色:
■源角色Adaptee:需要适配的目标或接口。
■目标角色Target:所期望得到的接口。
■适配器角色Adapter:适配类是本模式的核心,用来把源接口转换成目标接口,显然这三者的相互关系组成了适配器模式的原型。
【转载使用,请注明出处:http://blog.csdn.net/mahoking】
适配器模式也叫做包装器模式(Wrapper),根据适配器对象的不同,又可以将适配器模式为3种子类。
1、 类的适配器模式:用于对类的进行适配。
2、 对象的适配器模式:用于对对象进行包装。
3、 接口的适配器模式:用于对接口的抽象化。
第一种:类的适配群器模式。
/** * 源角色 * @author Mahc * */ public class Adaptee { /** * Adaptee原始类的方法 */ public void operate(){ System.out.println("Adaptee.operate()"); } } public interface Targetable { /** * 与Adaptee原始类相同的接口函数 */ public void operate(); /** *新的接口函数,Adaptee原始类中不存在 */ public void operateNew(); } public class Adapter extends Adaptee implements Targetable{ @Override public void operateNew() { // operate(); System.out.println("Adapter 在执行 Adaptee.operate()"); } } public class ClassAdapterPattern { public static void main(String[] args) { Adapter adapter = new Adapter(); adapter.operateNew(); } }
第二种:对象的适配器模式。
/** * 包装器模式 * @author Mahc * */ public class Wrapper implements Targetable{ private Adaptee adaptee; public Wrapper(Adaptee adaptee) { super(); this.adaptee = adaptee; } /** * 调用Adaptee原始类的方法 */ @Override public void operate() { adaptee.operate(); } /** * 实现目标类的新接口函数 */ @Override public void operateNew() { // adaptee.operate(); System.out.println("Wrapper 在执行 Adaptee.operate()"); System.out.println("包装目标类后的方法"); } } public class ObjectAdapterPattern { public static void main(String[] args) { Adaptee adaptee = new Adapter(); Wrapper wrapper = new Wrapper(adaptee); wrapper.operateNew(); } }
第三种:接口的适配器模式。
Sourceable是定义了多个接口函数的接口类。
DefaultAdapter是一个抽象类,它实现了接口Sourceable,并且为每个接口函数提供默认的实现。依据DefaultAdapter就可以编写不同的实现,在实现中只需要重写部分待实现的函数,而并非要重写全部。
/** * 源接口 * @author Mahc * */ public interface Sourceable { public void operate(); public void oprateNew(); } /** * 默认的适配器类DefaultAdapter * @author Mahc * */ public abstract class DefaultAdapter implements Sourceable{ @Override public void operate() { } @Override public void oprateNew() { } } public class NewAdapter extends DefaultAdapter{ @Override public void oprateNew() { System.out.println("源接口一个继承子类,覆盖原oprateNew!"); } } public class SourceAdapter extends DefaultAdapter{ @Override public void operate() { System.out.println("源接口一个继承子类,覆盖原operate!"); } } public class InterfaceAdapterPattern { public static void main(String[] args) { SourceAdapter sourceAdapter = new SourceAdapter(); sourceAdapter.operate(); NewAdapter newAdapter = new NewAdapter(); newAdapter.oprateNew(); } }
【转载使用,请注明出处:http://blog.csdn.net/mahoking】
相关文章推荐
- 结构型设计模式---Adapter模式(适配器模式)
- 设计模式[3] Adapter 适配器模式
- 设计模式c#语言描述——适配器模式
- Head First 设计模式之适配器模式学习
- Java设计模式之适配器模式
- 深入浅出Java设计模式之适配器模式
- 设计模式学习(七)适配器模式
- 设计模式1-适配器模式(Adapter)
- 设计模式 - Adapter Pattern(适配器模式)
- 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)
- 结合实现谈设计模式——适配器模式
- 深入浅出Java设计模式之适配器模式
- 设计模式——适配器模式
- .NET设计模式-适配器模式(Adapter Pattern)
- Dot Net设计模式—适配器模式
- 设计模式c#语言描述——建造者模式、适配器模式、合成(Composite)模式
- 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)
- Java设计模式之适配器模式
- .NET设计模式(8):适配器模式(Adapter Pattern)
- 设计模式(9)-适配器模式(Adapter)