Adapter Pattern
- 描述:提供一种解决方案,使不兼容的接口间能够互相协调工作,而不需要对已有接口进行修改。
- 与之相关的设计模式:Bridge Pattern, Decorator Pattern
Adapter Pattern可以通过两种方式实现(即:达到接口间数据类型格式转换的目的),一种是通过继承的方式实现,一种是使用委托聚合的方式实现。
- 采用继承的方式实现Adapter Pattern:
Adapter Pattern对应的类图:
在实现过程中,Adapter的作用在以下的Java代码中可以得到体现:
//Main
public static void main(String[] args)
{
XyzInterface xyz = new Adapter("xyz");
xyz.action();
}
//XyzInterface.java
public interface XyzInterface()
{
public void action(String arg);
}
//Adapter.java
public class Adapter extends ActionEntity implements XyzInterface()
{
public void action(String arg)
{
return this.entityAction(arg);
}
}
//ActionEntity.java
public class ActionEntity
{
public String entityAction(String arg)
{
return "<"+arg+">";
}
}
- 采用聚合的方式实现Adapter Pattern:
Adapter Pattern对应的类图:
在实现过程中,Adapter的作用在以下的Java代码中可以得到体现:
public static void main(String[] args)
{
XyzInterface xyz = new Adapter("xyz");
xyz.action();
}
//XyzInterface.java
public interface XyzInterface()
{
public void action(String arg);
}
//Adapter.java
public class Adapter implements XyzInterface()
{
private ActionEntity actionEntity;
public Adapter()
{
actionEntity = new ActionEntity();
}
public void action(String arg)
{
return actionEntity.entityAction(arg);
}
}
//ActionEntity.java
public class ActionEntity
{
public String entityAction(String arg)
{
return "<"+arg+">";
}
}
1、在实际开发过程中经常需要利用一些已经写好的类,但是这些已经写好的类还不能完全符合调用接口的要求,写一个Adapter作为二者之间的转化就可以发挥Adpater Pattern的作用。关键的是不用重新开发新代码或拷贝出冗余的代码出来,用Adapter类做一个转化即可。就像所谓的12V电压升压为24V一样。
2、Adapter Pattern并不只能用到有自己开发的接口上,也能用到别人开发的接口上,只要知道这些接口的规范即可。
3、Adapter Pattern的另一个用武之地就是在解决版本更新和兼容问题上。可以保证新旧版本共存的前提下对新版本进行维护升级。
- 大小: 27.3 KB
- 大小: 9.4 KB
分享到:
相关推荐
②设计模式之Adapter模式
javascript设计模式之Adapter模式【适配器模式】实现方法示例.docx
生活中有很多Adapter的例子... Adapter模式应该是设计模式中一个轻量级的模式,实现起来也比较简单,有时候不经意中,你可能就已经实现了一个Adapter模式,只是自己没有发现而已。本文将通过一个范例介绍 Adapter模式。
设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式之 适配器 Adapter C++ 源码 vs2019 工具,设计模式...
本文实例讲述了javascript设计模式之Adapter模式。分享给大家供大家参考,具体如下: 所谓Adapter模式就是适配器模式,主要是指使两个原本没有关联的类结合一起使用。 JS实现Adapter模式示例如下: <!DOCTYPE ...
设计模式1,FACADE模式,Adapter facade Adapter singleton proxy
设计模式之适配器模式(Adapter Pattern) 将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
设计模式之Adapter(适配器) 设计模式之Composite(组合) 设计模式之Decorator(油漆工) 设计模式之Bridge 设计模式之Flyweight(享元) 行为模式: 设计模式之Template 设计模式之Memento(备忘机制) 设计模式之Observer ...
设计模式C++学习之适配器模式(Adapter)
介绍24设计模式中的Adapter模式,如果使用,实例讲解,等等
java中的适配器模式,Adapter设计模式(包装器)23种设计模式之一
设计模式之Adapter(适配器) 设计模式之Composite(组合) 设计模式之Decorator(油漆工) 设计模式之Bridge 设计模式之Flyweight(享元) 行为模式: 设计模式之Template 设计模式之Memento(备忘机制) 设计模式之Observer ...
Head First 设计模式 (七) 适配器模式(Adapter pattern) C++实现
结构模式:设计模式之Facade(外观),设计模式之Proxy(代理),设计模式之Adapter(适配器),设计模式之Composite(组合),设计模式之Decorator(油漆工),设计模式之Bridge,设计模式之Flyweight(享元). 行为模式:设计模式之...
23种设计模式之七(结构型模式)Adapter 模式
走进设计模式之——适配器模式 (Adapter) 对应代码
主要介绍了java设计模式之适配器模式Adapter的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...
设计模式原代码 ADAPTER 将一个接口转成另一个接口, 方便不同系统之间的数据调用!