ADAPTER 适配器模式
在 GoF 的经典著作《设计模式》中指出: Adapter 模式的目的就是将类的接口转换成客户对象需要的接口, Adapter 模式使原本不相容的接口变的相容。也就是说我们有一个可以满足我们需要的对象,但是她的接口却不是象我们所期望的那样,而我们现在所需要的就是创建一个新的接口,让原本的接口能够满足我们的需要。
适用性
1:你想适用一个已经存在的类,而它的接口不符合你的需求;
2:你想创建一个复用的类,改类可以与其它不相关的类或不可预见的类协同工作;
3:你想使用一些已经存在的子类,对象适配器可以适配它的父类接口;
事例代码如下:
#include <iostream>
using namespace std;
class IDrawStrategy {
public:
virtual void draw() const = 0;
};
class Grapher {
public:
Grapher(IDrawStrategy* drawStrategy = 0) : _drawStrategy(drawStrategy) {}
public:
void drawShape() const;
void setShape(IDrawStrategy* drawStrategy);
protected:
IDrawStrategy* _drawStrategy;
};
void Grapher::drawShape() const {
if (_drawStrategy)
_drawStrategy->draw();
}
void Grapher::setShape(IDrawStrategy* drawStrategy) {
_drawStrategy = drawStrategy;
}
class IPaint {
public:
virtual void paint() const = 0;
};
class Triangle : public IPaint {
public:
void paint() const;
};
void Triangle::paint() const {
cout << "Draw Triangle" << endl;
}
class Circle : public IPaint {
public:
void paint() const;
};
void Circle::paint() const {
cout << "Draw Circle" << endl;
}
class Square : public IPaint {
public:
void paint() const;
};
void Square::paint() const {
cout << "Draw Square" << endl;
}
class DrawAdapter : public IDrawStrategy {
public:
DrawAdapter(IPaint* adaptee = 0) : _adaptee(adaptee) {}
virtual void draw() const;
protected:
IPaint* _adaptee;
};
void DrawAdapter::draw() const {
_adaptee->paint();
}
int main() {
Grapher grapher(&DrawAdapter(&Triangle()));
grapher.drawShape();
grapher.setShape(&DrawAdapter(&Circle()));
grapher.drawShape();
grapher.setShape(&DrawAdapter(&Square()));
grapher.drawShape();
getchar();
return 0;
}事例代码如下:
#include <iostream>
using namespace std;
class IDrawStrategy {
public:
virtual void draw() const = 0;
};
class Grapher {
public:
Grapher(IDrawStrategy* drawStrategy = 0) : _drawStrategy(drawStrategy) {}
public:
void drawShape() const;
void setShape(IDrawStrategy* drawStrategy);
protected:
IDrawStrategy* _drawStrategy;
};
void Grapher::drawShape() const {
if (_drawStrategy)
_drawStrategy->draw();
}
void Grapher::setShape(IDrawStrategy* drawStrategy) {
_drawStrategy = drawStrategy;
}
class IPaint {
public:
virtual void paint() const = 0;
};
class Triangle : public IPaint {
public:
void paint() const;
};
void Triangle::paint() const {
cout << "Draw Triangle" << endl;
}
class Circle : public IPaint {
public:
void paint() const;
};
void Circle::paint() const {
cout << "Draw Circle" << endl;
}
class Square : public IPaint {
public:
void paint() const;
};
void Square::paint() const {
cout << "Draw Square" << endl;
}
template<typename T>
class DrawAdapter : public IDrawStrategy, private T {
public:
virtual void draw() const;
};
template<typename T>
void DrawAdapter<T>::draw() const {
paint();
}
int main() {
Grapher grapher(&DrawAdapter<Triangle>());
grapher.drawShape();
grapher.setShape(&DrawAdapter<Circle>());
grapher.drawShape();
grapher.setShape(&DrawAdapter<Square>());
grapher.drawShape();
getchar();
return 0;
}
1572

被折叠的 条评论
为什么被折叠?



