Layering & Contract Philosophy With additional indirection

Adaptee object just is as a member. Adaptee object just relies on Adapter object.

  1. class CAdaptee
  2. {
  3. public: void theExistingAndIncompatibleMethod(){ } ;
  4. }
  5. class CTarget
  6. {
  7. public: virtual void theNewMethod() = ;
  8. }
  9.  
  10. class CAdapter: public CTarget, CAdaptee
  11. {
  12. public: void theNewMethod()
  13. {
  14. this->theExistingAndIncompatibleMethod();
  15. };
  16. }
  17.  
  18. class CAdapter: public CTarget
  19. {
  20. public: void theNewMethod()
  21. {
  22. CAdaptee object;
  23. object.theExistingAndIncompatibleMethod();
  24. };
  25. }
  26. class client
  27. {
  28. public: void operation()
  29. {
  30. CTarget *pTarget = new CAdapter; pTarget->theNewMethod();}
  31. }
  1.  

Applicability

Use the Adapter pattern when

  • you want to use an existing class having a incompatible method with the new interface from the one you want to use, and its interface does not match the one you need. You want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces.
  • (object adapter only) you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing/making as subclass every one. An object adapter can adapt the interface of its parent class.

Participants

  • Target (Shape): defines the domain-specific interface that Client uses.
  • Client (DrawingEditor): collaborates with objects conforming to the Target interface.
  • Adaptee (TextView): defines an existing interface that needs adapting.
  • Adapter (TextShape): adapt the interface of Adaptee to the Target interface.

Collaborations

  • Clients call operations on an Adapter instance. In turn, the adapter calls Adaptee operations that carry out the request.
  1.  

Design Pattern ->Adaptor的更多相关文章

  1. Design Pattern Adaptor 适配器设计模式

    适配器设计模式是为了要使用一个旧的接口,或许这个接口非常难用,或许是和新的更新的接口不兼容,所以须要设计一个适配器类,然后就能够让新旧的接口都统一. 就是这种一个图: watermark/2/text ...

  2. 说说设计模式~大话目录(Design Pattern)

    回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...

  3. 设计模式(Design Pattern)系列之.NET专题

    最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...

  4. [转]Design Pattern Interview Questions - Part 4

    Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...

  5. [转]Design Pattern Interview Questions - Part 2

    Interpeter , Iterator , Mediator , Memento and Observer design patterns. (I) what is Interpreter pat ...

  6. [转]Design Pattern Interview Questions - Part 3

    State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...

  7. [转]Design Pattern Interview Questions - Part 1

    Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...

  8. design pattern

    1. visitor design pattern http://butunclebob.com/ArticleS.UncleBob.IuseVisitor

  9. Design Pattern: Observer Pattern

    1. Brief 一直对Observer Pattern和Pub/Sub Pattern有所混淆,下面打算通过这两篇Blog来梳理这两种模式.若有纰漏请大家指正. 2. Use Case 首先我们来面 ...

随机推荐

  1. 基础线程机制--Daemon,sleep(),yield()

    Daemon   守护线程是程序运行时在后台提供服务的线程,不属于程序中不可或缺的部分,当所有非守护进程执行完成时,程序也就终止,同时会杀死所有的守护进程.main()属于非守护线程.可以使用setD ...

  2. java 在web应用中获取本地目录和服务器上的目录不一致的问题

    先来讲讲我所遇到的问题.最近有个新的项目添加新的功能. 修改之后部署到服务器上面发现取到classpath目录跑到别的地方去了.在本地测试却正常. 当时毛的着火了.硬是想不懂什么问题. 终于发现了这个 ...

  3. MySQL 关联查询  外连接 { LEFT| RIGHT } JOIN

    左外连接: (以左表为基准)两张表做连接的时候,在连接条件不匹配的时候留下左表中的数据,而右表中的数据以NULL填充例:使用左连接把学生的数据全取出来,该学生没有学院信息的用NULL填充 mysql& ...

  4. python BeautifulSoup基本用法

    #coding:utf-8 import os from bs4 import BeautifulSoup #jsp 路径 folderPath = "E:/whm/google/src_j ...

  5. git学习---去除版本控制

    本地这样去除文件夹 node_modules 的版本关联:执行:git rm -r --cached "node_modules/"提交: git commit -am 'remo ...

  6. js css 点亮 星级评分

    利用css 和 js 实现星级评分 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  7. idea(1)-idea初装

    1.安装插件 Alibaba Java Coding Guidelines Free Mybatis plugin MyBatis Log Plugin Lombok pluginGsonFormat ...

  8. 练习五十六:for循环

    某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换 方法一: def o ...

  9. c#之GDI简单实现代码及其实例

    作业:文档形式 3到5页理解 1.理解 2.源代码解释(1到2页) 3.实现效果 项目地址: https://github.com/zhiyishou/polyer Demo:https://zhiy ...

  10. phpstrom的xdebug开启和yii2下的分页的链接

    phpstrom的xdebug开启 1.修改php.ini文件(修改完重启apaceh) xdebug.remote_enable = onxdebug.idekey= PHPSTROM [注意:远程 ...