码迷,mamicode.com
首页 > 其他好文 > 详细

适配器模式(Adapter pattern)

时间:2016-08-06 08:34:37      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

https://en.wikipedia.org/wiki/Adapter_pattern

It allows the interface of an existing class to be used as another interface.

It is often used to make existing classes work with others without modifying their source code.

应用场景:让一个已存在的类基于其他类正常工作,而且不需要修改他们的源码。

举例:笔记本电源适配器(Adapter),让笔记本(client)正常工作,使用交流电(Adaptee),不修改笔记本也不修改交流电。

技术分享      技术分享        技术分享

uml 类图:

技术分享

也就是:

Client类 有一个target 成员变量(最终向上转型,引用指向实现类Adapter的实例)

Target接口 有request() 方法

Adapter类 有一个 adaptee 成员变量

Adapter类 是Target接口 的实现类,实现request()方法,方法体中调用 Adaptee 类的specificRequest() 方法

总结:没有改变Client ,也没有改变 Adaptee 。

接口实现类摇身一变

 

适配器模式(Adapter pattern)

标签:

原文地址:http://www.cnblogs.com/zno2/p/5732935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!