1. 定义 http://en.wikipedia.org/wiki/Adapter_pattern

An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. The adapter design pattern is used when you want two different classes with incompatible interfaces to work together. Interfaces may be incompatible but the inner functionality should suit the need. The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.

2. 分类 http://en.wikipedia.org/wiki/Adapter_pattern

There are two types of adapter patterns:

Object Adapter pattern

In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.

Class Adapter pattern

This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing. It is typical for the expected interface to be created as a pure interface class, especially in languages such as Java that do not support multiple inheritance.

  

3. 实例 http://blog.chinaunix.net/uid-29140694-id-4138579.html

1. 类适配器

public interface Target {/*** 源角色也包含的方法*/public void operation1();/*** 原角色不包含的方法*/public void operation2();
}/*** 原角色实现类*/
public class Adaptee {public void operation1(){System.out.println("operation-1");}
}/*** 适配器角色*/
public class Adapter extends Adaptee implements Target{@Overridepublic void operation2() {System.out.println("operation-2");}
}

2. 对象适配器

/*** 抽象目标接口*/
public interface Target {/*** 购买游戏道具*/public void buyGameProps();
}
/*** 被适配对象 */
public class Rmb {/*** 金额属性*/public int money;/*** 构造方法* @param money*/public Rmb(int money){this.money = money;}public int getMoney() {return money;}public void setMoney(int money) {this.money = money;}}
/*** QQ币实现类(适配器角色)*/
public class QQCoin implements Target{/*** 人民币实体对象*/public Rmb rmbImpl;/*** QQ币数量*/public int count;/*** 构造方法* @param rmb*/public QQCoin(Rmb rmb){this.count = rmb.getMoney() * 10;}/*** 具体实现方法*/@Overridepublic void buyGameProps() {System.out.println("您购买了 " + count + " 个道具!");}}/*** 测试Main方法*/
public class TestMain {public static void main(String [] args){Rmb rmb = new Rmb(5);QQCoin coin = new QQCoin(rmb);coin.buyGameProps();}
}

对象适配器的优点:
(1)适配器模式可以理解成是在原有基础上的封装,不需要对原有程序进行改动,即可实现特定功能。
(2)对象适配器可以服务于多个源角色,便于程序的扩展。

转载于:https://www.cnblogs.com/davidwang456/p/3844925.html

Adapter pattern相关推荐

  1. 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)

    [索引页] [源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabcd 介绍 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本 ...

  2. adapter pattern java_适配器模式(Adapter Pattern)

    适配器模式(Adapter Pattern) 是作为两个不兼容的接口之间的桥梁. 例子:读卡器是作为内存卡和笔记本之间的是适配器,内存卡插入读卡器,读卡器再插入笔记本电脑. 参考资料: . 作用:将一 ...

  3. 极速理解设计模式系列:6.适配器模式(Adapter Pattern)

    四个角色:目标抽象类角色(Target).目标对象角色(Adapter).源对象角色(Adaptee).客户端角色(Client) 目标抽象类角色(Target):定义需要实现的目标接口 目标对象角色 ...

  4. php本地服务手机适配器,php适配器模式(adapter pattern)

    下午陪家人和小孩,晚上练起来. /* The adapter pattern allows the interface of an existing class to be used from ano ...

  5. 【设计模式】适配器模式 Adapter Pattern

    适配器模式在软件开发界使用及其广泛,在工业界,现实中也是屡见不鲜.比如手机充电器,笔记本充电器,广播接收器,电视接收器等等.都是适配器. 适配器主要作用是让本来不兼容的两个事物兼容和谐的一起工作.比如 ...

  6. DesignPattern_Java:Adapter Pattern

    适配器模式 Adapter Pattern 变压器模式 Convert the interface of a class into another interface clients expect.A ...

  7. 结构型设计模式 (1)—— 适配器模式(Adapter Pattern)

    文章目录 1.概述 2.适配器模式的简单应用 3.适配器模式应用场景与优缺点 4.类适配器 5.小结 参考文献 1.概述 使用设计模式可以提高代码的可复用性.可扩充性和可维护性.适配器模式(Adapt ...

  8. 设计模式(七): 通过转接头来观察适配器模式(Adapter Pattern)

    在前面一篇博客中介绍了"命令模式"(Command Pattern),今天博客的主题是"适配器模式"(Adapter Pattern).适配器模式用处还是比较多 ...

  9. Java Adapter Pattern(适配器模式)

    适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁.这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能.这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口 ...

最新文章

  1. 2.12 向量化更多例子-深度学习-Stanford吴恩达教授
  2. 在 Shell 脚本中跟踪调试命令的执行
  3. Webservice 的设计和模式
  4. 返回内容验签失败_邮件经常失败回弹很糟心?一定要知道这几个小知识
  5. c语言从源代码到可执行,从源代码到可执行文件
  6. Git 的origin和master分析 push/diff/head
  7. React中最基础的jsx语法
  8. 屏蔽Codeforces做题时的Problem tags提示
  9. 掌机汉化辅助工具——WQSG 最佳伴侣发布amp;源代码发布
  10. 5个音效素材网站,赶紧收藏
  11. 小试牛刀1:制作一个简单的导航栏页面
  12. 介绍一个很好的英语学习软件——单词风暴
  13. 部署描述符[D:\apache-tomcat-9.0.38\conf\Catalina\localhost\books.xml]时出错
  14. 这有清理C盘空间的妙招,学会了就不怕内存满了
  15. 处理任意类型链表模板
  16. 10.[STM32]ADC中断方式-采集MQ-3酒精传感器检测的酒精浓度
  17. 孩子stem教育特殊之处
  18. 为什么你的下一餐要交给机器人?
  19. Matlab画分段函数
  20. Java连接Access 2013

热门文章

  1. jq 点击按钮跳转到微信_【看这里】教你用微信小程序登陆全国青少年普法网,方便快捷!...
  2. crontab 半小时_?用猫粮诱惑,7名消防员花半小时救出困在夹墙间的喵星人
  3. python资源管理错误漏洞_国家信息安全漏洞库
  4. html5 项目案例_互动案例技术分析(3)
  5. 对c语言课程的心得体会,C语言课程设计心得体会
  6. 插入排序(含希尔排序)的C/C++实现
  7. 深入浅出让你理解什么是LLVM
  8. python 隐马尔可夫链
  9. python生产脚本_生产级部署 Python 脚本,日志收集、崩溃自启,一键搞定
  10. getdata提取曲线数据_Origin如何从图表中获取数据