Adapter设计模式
在设计模式里,适配器模式(Adapter pattern)是将一个类的接口转换成用户所期待的。一个适配使得因接口不兼容而不能在一起的工作的类工作在一起。做法是将有别与自己的接口包裹在一个已存在的类中。
如果你有一个存在的系统需要插入一个新的类库,但是新的类库并不能匹配你写的系统,如下图:
现在你不想更改存在的旧系统,新的类库也不能修改,这时候我们就需要写一个适配器了,用适配器来适配新类库的接口。如下图:
用了适配器后的整个系统如下图:
现实生活中也有很多适配器的例子,比如电源适配器和适用于苹果笔记本的各种转换器。电源适配器将家用的220v电压变成PC或者手机能使用的电压。
适配器模式属于结构型模式,实现适配器模式有两种方式:继承(inheritance,is-a关系)和组合(composition,has-a关系)。分别对应类适配器模式和对象适配器模式。
1.类适配器模式(继承)
这种的UML类图如下:
对应的实现代码如下:
package xj.pattern.adapter; interface Target{ public void request(); } //被适配对象 class Adaptee{ public void specificRequest(){ System.out.println("现在使用的电压是220V!"); } } //适配器对象 class Adapter extends Adaptee implements Target{ @Override public void request() { super.specificRequest(); System.out.println("电源电压转换中。。。"); System.out.println("转换成110V......"); } } public class Client1 { public static void main(String[] args) { Target target = new Adapter(); target.request(); } }
2.对象适配器模式(组合)
这种的UML类图如下:
这种结构的实现代码如下:
interface Target{ public void request(); } //被适配对象 class Adaptee{ public void specificRequest(){ System.out.println("现在使用的电压是220V!"); } } //适配器对象 class Adapter implements Target{ private Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; } public Adaptee getAdaptee() { return adaptee; } public void setAdaptee(Adaptee adaptee) { this.adaptee = adaptee; } @Override public void request() { adaptee.specificRequest(); System.out.println("电源电压转换中。。。"); System.out.println("转换成110V......"); } } public class Client2 { public static void main(String[] args) { Adaptee adaptee = new Adaptee(); //类似于Android中的数据源:ArrayList<Entity> Target target = new Adapter(adaptee); //类似于Android中的ArrayListAdapter; target.request(); } }
Gof23定义了两种Adapter模式的实现结构:类适配器和对象适配器。但类适配器采用"多继承"的实现方式带来了不良的高耦合,所以一般不推荐使用。对象适配器采用"对象组合"的方式,更符合松耦合的精神。
Adapter模式本身要求我们尽可能的使用"面向接口的编程"风格,这样才能在后期很方便的适配。
相关推荐
②设计模式之Adapter模式
javascript设计模式之Adapter模式【适配器模式】实现方法示例.docx
生活中有很多Adapter的例子... Adapter模式应该是设计模式中一个轻量级的模式,实现起来也比较简单,有时候不经意中,你可能就已经实现了一个Adapter模式,只是自己没有发现而已。本文将通过一个范例介绍 Adapter模式。
设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式...
设计模式之Adapter(适配器) 设计模式之Composite(组合) 设计模式之Decorator(油漆工) 设计模式之Bridge 设计模式之Flyweight(享元) 行为模式: 设计模式之Template 设计模式之Memento(备忘机制) 设计模式之Observer ...
设计模式之Adapter(适配器) 设计模式之Composite(组合) 设计模式之Decorator(油漆工) 设计模式之Bridge 设计模式之Flyweight(享元) 行为模式: 设计模式之Template 设计模式之Memento(备忘机制) 设计模式之Observer ...
本文实例讲述了javascript设计模式之Adapter模式。分享给大家供大家参考,具体如下: 所谓Adapter模式就是适配器模式,主要是指使两个原本没有关联的类结合一起使用。 JS实现Adapter模式示例如下: <!DOCTYPE ...
结构模式:设计模式之Facade(外观),设计模式之Proxy(代理),设计模式之Adapter(适配器),设计模式之Composite(组合),设计模式之Decorator(油漆工),设计模式之Bridge,设计模式之Flyweight(享元). 行为模式:设计模式之...
主要为大家详细介绍了Java设计模式之Adapter适配器模式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
设计模式1,FACADE模式,Adapter facade Adapter singleton proxy
设计模式之适配器模式(Adapter Pattern) 将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
设计模式之 Adapter(适配器) 使用类再生的两个方式:组合(new)和继承(extends),这个已经在 thinking in java中提到过. 设计模式之 Proxy(代理) 以 Jive 为例,剖析代理模式在用户级别授权机制上的应用 设计模式之 ...
介绍24设计模式中的Adapter模式,如果使用,实例讲解,等等
设计模式C++学习之适配器模式(Adapter)
java中的适配器模式,Adapter设计模式(包装器)23种设计模式之一
Head First 设计模式 (七) 适配器模式(Adapter pattern) C++实现
java 设计模式 设计模式之Adapter、设计模式之Bridge等等
23种设计模式之七(结构型模式)Adapter 模式
走进设计模式之——适配器模式 (Adapter) 对应代码