Prototype Pattern

The Prototype design pattern is used to create new objects by copying an existing object, known as the prototype, rather than creating them from scratch.

It allows you to create new instances without knowing their concrete classes. In simple terms, think of it as making clones of an object instead of creating new objects from the ground up.

The Prototype pattern is especially useful when the cost of creating a new object is high or when you want to isolate the object creation process from the rest of the code.

Prototype Pattern C++ Example:

Let’s see an example of the Prototype pattern using a Shape class as the prototype. We’ll demonstrate how to create different shapes by copying the prototype.

#include <iostream>
#include <string>

// Prototype interface
class Shape {
public:
    virtual Shape* clone() const = 0;
    virtual void draw() const = 0;
    virtual ~Shape() {}
};

// Concrete prototype: Circle
class Circle : public Shape {
public:
    Shape* clone() const override {
        return new Circle(*this);
    }

    void draw() const override {
        std::cout << "Drawing a Circle." << std::endl;
    }
};

// Concrete prototype: Square
class Square : public Shape {
public:
    Shape* clone() const override {
        return new Square(*this);
    }

    void draw() const override {
        std::cout << "Drawing a Square." << std::endl;
    }
};

int main() {
    // Create a Circle prototype
    Shape* circlePrototype = new Circle();

    // Create a Square prototype
    Shape* squarePrototype = new Square();

    // Create different shapes by cloning the prototypes
    Shape* shape1 = circlePrototype->clone();
    Shape* shape2 = squarePrototype->clone();

    // Draw the shapes
    shape1->draw(); // Output: Drawing a Circle.
    shape2->draw(); // Output: Drawing a Square.

    // Clean up
    delete circlePrototype;
    delete squarePrototype;
    delete shape1;
    delete shape2;

    return 0;
}

Prototype Pattern Code Explanation:
In this example, we have a Shape class, which serves as the prototype. It defines two virtual methods: clone() and draw(). The clone() method is used to create a copy of the prototype, and the draw() method is used to display the shape.

We then create two concrete classes, Circle and Square, which are implementations of the Shape prototype. Each concrete class provides its implementation for the clone() and draw() methods.

In the main() function, we demonstrate how the Prototype pattern works. We first create instances of Circle and Square, which act as prototypes for creating shapes. Then, we create new shapes by cloning the prototypes using the clone() method. This way, we can create different shapes without explicitly knowing their concrete classes.

The Prototype pattern allows you to create new objects by copying existing prototypes. It provides a way to create instances without knowing the specific classes and isolates the object creation process. By using the Prototype pattern, you can save resources and improve performance when creating objects, especially in situations where the object creation is expensive.