C++ 设计模式之装饰模式
目录
设计目的
动态地给一个对象添加一些额外的功能。
就增加功能来说,继承为类引入静态特征,并且随着扩展功能的增多,子类会很膨胀,反之装饰模式可以在不想增加很多子类的情况下扩展类。
UML设计
以绘制彩色形状为例:
代码实现
//.h文件
#ifndef SHAPE_H
#define SHAPE_H
#include <QObject>
class Shape : public QObject
{
Q_OBJECT
public:
virtual void draw() = 0;
explicit Shape(QObject *parent = nullptr);
};
#endif // SHAPE_H
//.cpp文件
#include "Shape.h"
Shape::Shape(QObject *parent) : QObject(parent)
{
}
//.h文件
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <QObject>
#include "Shape.h"
class Rectangle : public Shape
{
Q_OBJECT
public:
explicit Rectangle(Shape *parent = nullptr);
virtual void draw();
};
#endif // RECTANGLE_H
//.cpp文件
#include "Rectangle.h"
#include <iostream>
using namespace std;
Rectangle::Rectangle(Shape *parent) : Shape(parent)
{
}
void Rectangle::draw()
{
std::cout << "Shape draw Rectangle" << endl;
}
//.h文件
#ifndef CIRCLE_H
#define CIRCLE_H
#include <QObject>
#include "Shape.h"
class Circle : public Shape
{
Q_OBJECT
public:
explicit Circle(Shape *parent = nullptr);
virtual void draw();
};
#endif // CIRCLE_H
//.cpp文件
#include "Circle.h"
#include <iostream>
using namespace std;
Circle::Circle(Shape *parent) : Shape(parent)
{
}
void Circle::draw()
{
std::cout << "Shape draw circle" << endl;
}
//.h文件
#ifndef COLORSHAPE_H
#define COLORSHAPE_H
#include <QObject>
#include "Shape.h"
class ColorShape : public Shape
{
Q_OBJECT
public:
explicit ColorShape(Shape *parent = nullptr);
virtual void setShape(Shape* shape) = 0;
virtual void draw() = 0;
};
#endif // COLORSHAPE_H
//.cpp文件
#include "ColorShape.h"
ColorShape::ColorShape(Shape *parent) : Shape(parent)
{
}
//.h文件
#ifndef REDSHAPE_H
#define REDSHAPE_H
#include <QObject>
#include "ColorShape.h"
class RedShape : public ColorShape
{
Q_OBJECT
public:
explicit RedShape(ColorShape *parent = nullptr);
virtual void draw();
virtual void setShape(Shape* ptrShape);
private:
Shape* m_ptrShape;
private:
void drawRedColor();
};
#endif // REDSHAPE_H
//.cpp文件
#include "RedShape.h"
#include <iostream>
using namespace std;
RedShape::RedShape(ColorShape *parent) : ColorShape(parent),
m_ptrShape(nullptr)
{
}
void RedShape::draw()
{
drawRedColor();
m_ptrShape->draw();
}
void RedShape::setShape(Shape *ptrShape)
{
m_ptrShape = ptrShape;
}
void RedShape::drawRedColor()
{
std::cout << "RedShape draw Red color"<< endl;
}
//.h文件
#ifndef GREENSHAPE_H
#define GREENSHAPE_H
#include <QObject>
#include "ColorShape.h"
class GreenShape : public ColorShape
{
Q_OBJECT
public:
explicit GreenShape(ColorShape *parent = nullptr);
virtual void draw();
virtual void setShape(Shape* ptrShape);
private:
Shape* m_ptrShape;
private:
void drawGreenColor();
};
#endif // GREENSHAPE_H
//.cpp文件
#include "GreenShape.h"
#include <iostream>
using namespace std;
GreenShape::GreenShape(ColorShape *parent) : ColorShape(parent),
m_ptrShape(nullptr)
{
}
void GreenShape::draw()
{
drawGreenColor();
m_ptrShape->draw();
}
void GreenShape::setShape(Shape *ptrShape)
{
m_ptrShape = ptrShape;
}
void GreenShape::drawGreenColor()
{
std::cout << "GreenShape draw Green color" << endl;
}
//main.cpp文件调用
#include <QCoreApplication>
#include "Circle.h"
#include "Rectangle.h"
#include "GreenShape.h"
#include "RedShape.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Shape* circle = new Circle;
Shape* rectangle = new Rectangle;
ColorShape* redShape = new RedShape;
ColorShape* greenShape = new GreenShape;
redShape->setShape(circle);
redShape->draw();
greenShape->setShape(rectangle);
greenShape->draw();
return a.exec();
}