模式 / 创建型模式
image

#include <iostream>
using namespace std;

//Prototype
class Shape  {
public:
    string color;

    virtual Shape* clone() = 0;
};

//ConcretePrototype
class Squarepublic Shape {
public:
    Square(string color) {
        this->color = color;
    }

    Square(Shape const &shape) {
        this->color = shape.color;
    }

    Shape* clone() {
        return new Square(*this);
    }
};

//Client
class ShapeMaker {
    Shape* shape;

public:
    ShapeMaker(Shape* shape) {
        this->shape = shape;
    }

    Shape* makeShape() {
        return shape->clone();
    }
};

Square square("Red");
ShapeMaker maker(&square);

Shape* square1 = maker.makeShape();
Square* square2 = (Square*)maker.makeShape();

cout << "color1 is "  << square1->color << endl;
cout << "color2 is "  << square2->color;