策略模式和C++多态
策略模式: 设计模式。一种在运行时更改算法行为的方式。
多态: 语言机制。通过基类的指针或者引用,在运行时动态调用实际绑定对象函数的行为。
多态是设计模式的基础,多态是框架的基础。
C++ 中实现策略模式有两种方法:一是多态;一是模板。
- 多态实现策略模式
// 多态实现策略模式
#include <iostream>
// 几何操作算法集
class GeometryStrategy {
public:
// 平移
virtual void offset(int x, int y) = 0;
// 其他操作
};
// 点
class PointStrategy : public GeometryStrategy {
public:
PointStrategy(int x, int y) : m_x(x), m_y(y) {}
PointStrategy(const PointStrategy &p) {
m_x = p.x();
m_y = p.y();
}
int x() const {return m_x;}
int y() const {return m_y;}
virtual void offset(int x, int y) {
m_x += x;
m_y += y;
}
void offset(PointStrategy pt) {
offset(pt.x(), pt.y());
}
private:
int m_x;
int m_y;
};
// 矩形
class RectangleStrategy : public GeometryStrategy{
public:
RectangleStrategy(PointStrategy bottomLeft,
PointStrategy topRight) :
m_min (bottomLeft), m_max (topRight) {}
virtual void offset(int x, int y) override {
m_min.offset(x, y);
m_max.offset(x, y);
}
private:
PointStrategy m_min;
PointStrategy m_max;
};
// 圆
class CircleStrategy : public GeometryStrategy {
public:
CircleStrategy(PointStrategy center, int radius) : m_center(center), m_r(radius) {}
virtual void offset(int x, int y) override {
m_center.offset(x, y);
}
private:
PointStrategy m_center;
int m_r;
};
// 定义一个上下文
class Context {
public:
// 抽象类不能实例化,这里声明为引用。
Context(GeometryStrategy &geometry) {
m_geometry = &geometry;
}
void offsetGeometry(PointStrategy pt) {
m_geometry->offset(pt.x(), pt.y());
}
private:
GeometryStrategy *m_geometry;
};
int main() {
PointStrategy pt0(0,0);
PointStrategy pt1(10,10);
PointStrategy offset(10, 20);
RectangleStrategy rectangle(pt0, pt1);
Context context1(rectangle);
context1.offsetGeometry(offset);
CircleStrategy circle(pt0, 5);
Context context2(circle);
context2.offsetGeometry(offset);
return 0;
}
- 类模板实现策略模式
(这里图方便就直接拿上面的例子进行修改,可能不是很恰当)
// 类模板实现策略模式
// 点
class PointStrategy {
public:
PointStrategy(int x, int y) : m_x(x), m_y(y) {}
PointStrategy(const PointStrategy &point) {
m_x = point.x();
m_y = point.y();
}
int x() const { return m_x;}
int y() const { return m_y;}
void offset(int x, int y) {
m_x += x;
m_y += y;
}
private:
int m_x;
int m_y;
};
// 矩形
class RectangleStrategy {
public :
RectangleStrategy(PointStrategy bottomLeft, PointStrategy topRight) :
m_min(bottomLeft), m_max(topRight){}
void offset(int x, int y) {
m_min.offset(x, y);
m_max.offset(x, y);
}
private :
PointStrategy m_min;
PointStrategy m_max;
};
// 圆
class CircleStrategy {
public:
CircleStrategy(PointStrategy center, int radius) :
m_center(center), m_r(radius) {}
void offset(int x, int y) {
m_center.offset(x, y);
}
private:
PointStrategy m_center;
int m_r;
};
// 定义了一个上下文
template<class Strategy>
class Context
{
public:
Context(Strategy &strategy) : strtgy(strategy) {}
void offsetGeometry(PointStrategy pt)
{
strtgy.offset(pt.x(), pt.y());
}
private:
Strategy strtgy;
};
int main() {
PointStrategy pt0(0,0);
PointStrategy pt1(10,10);
PointStrategy offset(10, 20);
RectangleStrategy rectangle(pt0, pt1);
Context<RectangleStrategy> strategy1(rectangle);
strategy1.offsetGeometry(offset);
CircleStrategy circle(pt0, 5);
Context<CircleStrategy> strategy2(circle);
strategy2.offsetGeometry(offset);
return 0;
}