意图:
将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作
适用性:
1想使用一个已经存在的类,而它的几口不附和要求
2创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类协同工作、3想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口,adapter可以适配它的父类接口
适配器的目的是将已有类的功能提供给客户类,是一个中间者的角色。类适配器是客户类有一个接口规范的情况下可用,此时适配类只需作为功能类的子类,并实现接口并可,直接用功能类实现了客户类的要求。而对象适配类是在客户类没有提供接口的情况下用的,适配类作为客户类的子类,并在其中实例化一个功能类的对象,并调用此对象的方法实现适配,故称对象适配。
效果:
类适配器:
1用一个具体的Adapter类对Adpater和Target进行匹配
2Adpter可以重定义Adptee的行为,因为Adapter是Adaptee的子类
3仅仅引入一个对象,并不需要额外的指针以间接得到adaptee
对象适配器
1允许一个Adapter和多个Adaptee同时工作
2重定义Adaptee比较困难,因为引用了Adaptee本身
实现:
1使用C++实现适配器类 Adapter类采用公共方式继承Target类,并且用私有方式继承Adaptee类
2可插入的适配器 a使用抽象操作 b使用代理对象c参数化的适配器
其代码实现如下:
Adapter.h
- #include <iostream>
- using namespace std;
- class Point
- {
- public:
- Point(int i,int j):x(i),y(j){}
- private:
- int x;
- int y;
- };
- class Manipulator
- {
- public:
- Manipulator(){cout<<"A new Manipulator is created"<<endl;}
- };
- class TextManipulator :public Manipulator
- {
- public:
- TextManipulator(){cout<<"A new TextManipulator is created"<<endl;}
- };
- class Shape
- {
- public:
- Shape(){cout<<"Shape is created"<<endl;};
- virtual void BoundingBox(Point& bottomLeft,Point& topRight) const
- {
- cout<<"Bounding Box for Shape is created;"<<endl;
- }
- virtual Manipulator* CreateManipulator() const
- {
- return new Manipulator();
- }
- };
- class TextView
- {
- public:
- TextView(){cout<<"Text View is created"<<endl;}
- void GetOrigin(int& x,int& y) const {x=3;y=4;}
- void GetExtent(int& width,int& height) const {width=10;height=10;}
- virtual bool IsEmpty() const {return true;}
- };
- class TextShape:public Shape,private TextView
- {
- public:
- TextShape(){cout<<"Text Shape is created"<<endl;}
- virtual void BoundingBox(Point& bottomLeft,Point& topRight) const;
- virtual Manipulator* CreateManipulator() const
- {
- return new TextManipulator();
- }
- virtual bool IsEmpty() const;
- };
- class TextShape2:public Shape
- {
- public:
- TextShape2(TextView* t){_text=t;}
- virtual void BoundingBox(Point& bottomLeft,Point& topRight) const;
- /*virtual Manipulator* CreateManipulator() const
- {
- return new TextManipulator();
- }*/
- virtual bool IsEmpty() const;
- private:
- TextView* _text;
- };
Adapter.cpp
- #include "Adapter.h"
- #include <iostream>
- using namespace std;
- void TextShape::BoundingBox(Point& bottomLeft,Point& topRight) const
- {
- int bottom,left,width,height;
- GetOrigin(bottom,left);
- GetExtent(width,height);
- bottomLeft = Point(bottom,left);
- topRight = Point(bottom+height,left+width);
- cout<<"Bounding Box for TextShape is created,bottom is"<<bottom<<" left is"/
- <<left<<" height is "<<height<< " width is "<< width;
- }
- bool TextShape::IsEmpty() const
- {
- return TextView::IsEmpty();
- }
- void TextShape2::BoundingBox(Point& bottomLeft,Point& topRight) const
- {
- int bottom,left,width,height;
- _text->GetOrigin(bottom,left);
- _text->GetExtent(width,height);
- bottomLeft = Point(bottom,left);
- topRight = Point(bottom+height,left+width);
- cout<<"Bounding Box for TextShape is created,bottom is"<<bottom<<" left is"/
- <<left<<" height is "<<height<< " width is "<< width;
- }
- bool TextShape2::IsEmpty() const
- {
- return _text->IsEmpty();
- }
main.cpp
- #include "Adapter.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- TextShape b;
- Point a(1,2),c(3,4);
- b.BoundingBox(a,c);
- TextView e;
- TextShape2 d(&e);
- d.BoundingBox(a,c);
- return 0;
- }