C++ 设计模式之装饰模式

C++ 设计模式之装饰模式

目录

C++ 设计模式之装饰模式

设计目的

UML设计

代码实现


 

设计目的

动态地给一个对象添加一些额外的功能。

就增加功能来说,继承为类引入静态特征,并且随着扩展功能的增多,子类会很膨胀,反之装饰模式可以在不想增加很多子类的情况下扩展类。

UML设计

以绘制彩色形状为例:

C++ 设计模式之装饰模式_第1张图片

代码实现

//.h文件

#ifndef SHAPE_H
#define SHAPE_H

#include 

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 

#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 
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 

#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 
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 
#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 
#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 
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 
#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 
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 
#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();
}

 

你可能感兴趣的:(C++设计模式)