1. 实现类
1.1. Implementor(实现类接口)
//Implementor(实现类接口)
class Color
{
public:
virtual ~Color() = default;
virtual void fillColor() = 0;
private:
};
1.2. ConcreteImplementor(具体实现类),Red,Green,Blue
//ConcreteImplementor(具体实现类),Red,Green,Blue
class RedColor : public Color
{
public:
RedColor()
{
std::cout << "RedColor 构造函数" << std::endl;
m_sColorType = "Red";
}
~RedColor()
{
std::cout << "~RedColor 析构函数" << std::endl;
}
void fillColor() override
{
std::cout << "填充颜色;" << m_sColorType << std::endl;
}
private:
std::string m_sColorType;
};
class GreenColor : public Color
{
public:
GreenColor()
{
std::cout << "GreenColor 构造函数" << std::endl;
m_sColorType = "Green";
}
~GreenColor()
{
std::cout << "~GreenColor 析构函数" << std::endl;
}
void fillColor() override
{
std::cout << "填充颜色;" << m_sColorType << std::endl;
}
private:
std::string m_sColorType;
};
class BlueColor : public Color
{
public:
BlueColor()
{
std::cout << "BlueColor 构造函数" << std::endl;
m_sColorType = "Blue";
}
~BlueColor()
{
std::cout << "~BlueColor 析构函数" << std::endl;
}
void fillColor() override
{
std::cout << "填充颜色;" << m_sColorType << std::endl;
}
private:
std::string m_sColorType;
};
2. 抽象类
2.1. Abstraction(抽象类)
//Abstraction(抽象类)
class Shape
{
public:
virtual ~Shape() = default;
virtual void showShape() = 0;
virtual void chooseColor(Color* col) = 0;
protected:
virtual void drawShape() = 0;
virtual void drawColor() = 0;
Color* m_pColor = nullptr;
};
2.2. RefinedAbstraction(扩充抽象类)Circle, Rectangle
//RefinedAbstraction(扩充抽象类)Circle, Rectangle,
class CircleShape : public Shape
{
public:
CircleShape()
{
std::cout << "CircleShape 构造函数" << std::endl;
m_sShapeType = "Circle";
}
~CircleShape()
{
std::cout << "~CircleShape 析构函数" << std::endl;
if (nullptr != m_pColor)
{
delete m_pColor;
m_pColor = nullptr;
}
}
void showShape() override
{
drawShape();
drawColor();
}
void chooseColor(Color* col) override
{
m_pColor = col;
}
private:
void drawShape() override
{
std::cout << "绘制图形:" << m_sShapeType << std::endl;
}
void drawColor() override
{
m_pColor->fillColor();
}
std::string m_sShapeType;
};
class RectangleShape : public Shape
{
public:
RectangleShape()
{
std::cout << "CircleShape 构造函数" << std::endl;
m_sShapeType = "Rectangle";
}
~RectangleShape()
{
std::cout << "~CircleShape 析构函数" << std::endl;
if (nullptr != m_pColor)
{
delete m_pColor;
m_pColor = nullptr;
}
}
void showShape() override
{
drawShape();
drawColor();
}
void chooseColor(Color* col) override
{
m_pColor = col;
}
private:
void drawShape() override
{
std::cout << "绘制图形:" << m_sShapeType << std::endl;
}
void drawColor() override
{
m_pColor->fillColor();
}
std::string m_sShapeType;
};
3. 主函数
#include <list>
#include <string>
#include <iostream>
using namespace std;
void main()
{
Shape *shape;
//绘制 红色 圆形
std::cout << "=====绘制 红色 圆形=====" << std::endl;
shape = new CircleShape();
shape->chooseColor(new RedColor());
shape->showShape();
delete shape;
//绘制 绿色 圆形
std::cout << "=====绘制 绿色 圆形=====" << std::endl;
shape = new CircleShape();
shape->chooseColor(new GreenColor());
shape->showShape();
delete shape;
//绘制 蓝色 矩形
std::cout << "=====绘制 蓝色 矩形=====" << std::endl;
shape = new RectangleShape();
shape->chooseColor(new BlueColor());
shape->showShape();
delete shape;
system("pause");
}