设计模式笔记--结构型模式之一适配器 adapter

 

意图:

将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作

适用性:

1想使用一个已经存在的类,而它的几口不附和要求

2创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类协同工作、3想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口,adapter可以适配它的父类接口

 

适配器的目的是将已有类的功能提供给客户类,是一个中间者的角色。类适配器是客户类有一个接口规范的情况下可用,此时适配类只需作为功能类的子类,并实现接口并可,直接用功能类实现了客户类的要求。而对象适配类是在客户类没有提供接口的情况下用的,适配类作为客户类的子类,并在其中实例化一个功能类的对象,并调用此对象的方法实现适配,故称对象适配。

 

效果:

类适配器:

1用一个具体的Adapter类对AdpaterTarget进行匹配

2Adpter可以重定义Adptee的行为,因为AdapterAdaptee的子类

3仅仅引入一个对象,并不需要额外的指针以间接得到adaptee

对象适配器

1允许一个Adapter和多个Adaptee同时工作

2重定义Adaptee比较困难,因为引用了Adaptee本身

 

实现:

1使用C++实现适配器类 Adapter类采用公共方式继承Target类,并且用私有方式继承Adaptee

2可插入的适配器   a使用抽象操作 b使用代理对象c参数化的适配器

 

 

其代码实现如下:

Adapter.h

 

  1. #include <iostream>
  2. using namespace std;
  3. class Point
  4. {
  5. public:
  6.     Point(int i,int j):x(i),y(j){}
  7. private:
  8.     int x;
  9.     int y;
  10. };
  11. class Manipulator
  12. {
  13. public:
  14.     Manipulator(){cout<<"A new Manipulator is created"<<endl;}
  15. };
  16. class TextManipulator :public Manipulator
  17. {
  18. public:
  19.     TextManipulator(){cout<<"A new TextManipulator is created"<<endl;}
  20. };
  21. class Shape
  22. {
  23. public:
  24.     Shape(){cout<<"Shape is created"<<endl;};
  25.     virtual void BoundingBox(Point& bottomLeft,Point& topRight) const 
  26.     {
  27.         cout<<"Bounding Box for Shape is created;"<<endl;
  28.     }
  29.     virtual Manipulator* CreateManipulator() const
  30.     {
  31.         return new Manipulator();
  32.     }
  33. };
  34. class TextView
  35. {
  36. public:
  37.     TextView(){cout<<"Text View is created"<<endl;}
  38.     void GetOrigin(int& x,int& y) const {x=3;y=4;}
  39.     void GetExtent(int& width,int& height) const {width=10;height=10;}
  40.     virtual bool IsEmpty() const {return true;}
  41. };
  42. class TextShape:public Shape,private TextView  
  43. {
  44. public:
  45.     TextShape(){cout<<"Text Shape is created"<<endl;}
  46.     virtual void BoundingBox(Point& bottomLeft,Point& topRight) const;
  47.     virtual Manipulator* CreateManipulator() const
  48.     {
  49.         return new TextManipulator();
  50.     }
  51.     virtual bool IsEmpty() const;
  52. };
  53. class TextShape2:public Shape
  54. {
  55. public:
  56.     TextShape2(TextView* t){_text=t;}
  57.     virtual void BoundingBox(Point& bottomLeft,Point& topRight) const;
  58.     /*virtual Manipulator* CreateManipulator() const
  59.     {
  60.         return new TextManipulator();
  61.     }*/
  62.     virtual bool IsEmpty() const;
  63. private:
  64.     TextView* _text;
  65. };

 

Adapter.cpp

 

  1. #include "Adapter.h"
  2. #include <iostream>
  3. using namespace std;
  4. void TextShape::BoundingBox(Point& bottomLeft,Point& topRight) const
  5. {
  6.     int bottom,left,width,height;
  7.     GetOrigin(bottom,left);
  8.     GetExtent(width,height);
  9.     bottomLeft = Point(bottom,left);
  10.     topRight = Point(bottom+height,left+width);
  11.     cout<<"Bounding Box for TextShape is created,bottom is"<<bottom<<" left is"/
  12.         <<left<<" height is "<<height<< " width is "<< width;
  13. bool TextShape::IsEmpty() const
  14. {
  15.     return TextView::IsEmpty();
  16. }
  17. void TextShape2::BoundingBox(Point& bottomLeft,Point& topRight) const
  18. {
  19.     int bottom,left,width,height;
  20.     _text->GetOrigin(bottom,left);
  21.     _text->GetExtent(width,height);
  22.     bottomLeft = Point(bottom,left);
  23.     topRight = Point(bottom+height,left+width);
  24.     cout<<"Bounding Box for TextShape is created,bottom is"<<bottom<<" left is"/
  25.         <<left<<" height is "<<height<< " width is "<< width;
  26. bool TextShape2::IsEmpty() const
  27. {
  28.     return _text->IsEmpty();
  29. }

main.cpp

 

 

  1. #include "Adapter.h"
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.     TextShape b;  
  7.     Point a(1,2),c(3,4);
  8.     b.BoundingBox(a,c);
  9.     TextView e;
  10.     TextShape2 d(&e); 
  11.     d.BoundingBox(a,c);
  12.     return 0;
  13. }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值