适配器模式(类对象结构型模式)
来源:互联网 发布:跑步软件咕咚 编辑:程序博客网 时间:2024/06/05 20:52
意图:将一个类的借口转换成客户希望的另外一个接口。使得原来由于接口不兼容而不能一起工作的那些类可以一起工作。
适用性:
1 想使用一个已经存在的类,而它的借口不符合你的需求
2 创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类协同工作
3 (仅适用于对象Adapter)使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口。对象适配器可以适配它的父类接口。
结构:
类适配器使用多重继承对一个接口与另一个接口进行匹配。用C++实现时,Adapter应该采用公共方式继承Target类,并且用私有方式继承Adaptee。所以,Adapter类应该是Target的子类型,但不是Adaptee的子类型。
对象适配器依赖于对象组合,将Adaptee的一个实例作为Adapter类中一个成员,从而对Adapter的操作,转而调用Adaptee类的操作。
这种模式也可以提供那些被匹配的泪所没有提供的功能。
以下为书上98页例子:将TextView适配Shape的接口,有两种实现,一种构造一个类适配器,一种是对象适配器
#include <iostream>using namespace std;//点结构体struct Point{ Point(int x = 0, int y = 0) : _x(x), _y(y) {}private: int _x; int _y;};class Shape;class TextShape;//控制器,可以对Shape进行操控class Manipulator{public:Manipulator(){cout << "构造一个Manipulator" << endl;}};class TextManipulator : public Manipulator{public:TextManipulator(){cout << "构造一个TextManipulator" << endl;}};//接口class Shape{public: Shape() { cout << "创建一个Shape..." << endl; } virtual void BoundingBox(Point& bottomLeft, Point& topRight) const { } virtual Manipulator* CreateManipulator() const { cout << "创建了一个CreateMainputor" << endl; return new Manipulator(); }};//adaptee,不兼容的类,通过TextShape进行适配class TextView{public: TextView() { cout << "构造一个TextView" << endl; } void GetOrigin(int x, int y) const { x = 0; y = 0; } void GetExtent(int width, int height) const { width = 10; height = 10; } virtual bool IsEmpty() const { return false; }};//Adapter,使TextView适合Shape的接口 ,这是个类适配器,// 通过public继承接口,private继承实现class TextShape : public Shape, private TextView{public: TextShape() { cout << "构造一个TextShape" << endl; } virtual void 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); } bool IsEmpty() const { return TextView::IsEmpty(); } Manipulator* CreateManipulator() const { return new TextManipulator(); }};//对象适配器,包含了一个要适配的对象class TextShape1 : public Shape{public: TextShape1(TextView*) { cout << "构造一个TextShape1对象" << endl; } virtual void 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); } bool IsEmpty() const { return _text->IsEmpty(); } Manipulator* CreateManipulator() const { return new TextManipulator(); }private: TextView* _text;};
0 0
- 适配器模式(类对象结构型模式)
- 适配器模式(Adapter Pattern 类对象结构型模式)
- 7种结构型模式之:适配器模式(Adapter):类适配器、对象适配器
- Adapter(适配器)--类对象结构型模式
- 结构型模式之 类的适配器、对象适配器 笔记
- 适配器模式(Adapter)-对象结构型模式
- 2.1 Adapter(适配器) -- 类/对象结构型模式
- 适配器模式——类对象结构型模式
- 适配器模式(Adapter)(对象结构型模式)
- 设计模式11:对象适配器【结构型】
- 结构型模式之对象适配器
- 设计模式之适配器(Adapter)---类对象结构型模式
- 类/对象结构型模式——适配器模式(Apapter)
- 设计模式之九:Adapter(适配器)—类对象结构型模式
- 适配器模式(结构型)
- 适配器模式(结构型)
- 适配器模式(结构型)
- 适配器(Adapter)——类对象结构型模式
- [redis] setproctitle.c实现 修改进程名称
- 迪厅装修后地板清洁与保养
- 极佳的power design设计
- adb shell 命令详解
- 自作多情总是可悲
- 适配器模式(类对象结构型模式)
- 命令4-&&和||
- TCL脚本语言学习
- asterisk1.8 Makefile分析 (1)
- CWnd::MessageBoxW”: 不能将参数 1 从“const char [16]”转换为“LPCTSTR
- 深入学习python(一) lambda函数
- 个性酒吧花式调酒
- 依然亲爱的,你别来无恙。
- HTML5 Video 实现浅析