Skip to content

Latest commit

 

History

History
93 lines (70 loc) · 4.07 KB

prototype.md

File metadata and controls

93 lines (70 loc) · 4.07 KB

Prototype

The Prototype design pattern is a creational design pattern that allows cloning objects without coupling the code to their specific classes. It provides a way to create new objects by copying or cloning existing objects, known as prototypes, instead of creating them from scratch.

The Prototype pattern promotes object creation through cloning rather than through class instantiation. It is useful when creating new objects is more expensive or complex than copying existing objects, or when creating objects with specific initial states is impractical using normal construction methods.

Let's consider a real-world example of the Prototype design pattern in the context of a graphic design application.

Suppose you are building a graphic design tool that allows users to create and modify various shapes, such as circles, squares, and triangles. Each shape has its own properties (e.g., color, size, position) and can be customized by the user.

Instead of creating new shape objects from scratch every time a user wants to add a shape, you can use the Prototype pattern to clone an existing shape as a template and then modify its properties accordingly.

Here's how the code for this example might look in C#:

using System;

// The prototype interface
public interface IShape
{
    void Draw();
    IShape Clone();
}

// Concrete prototype classes
public class Circle : IShape
{
    public string Color { get; set; }
    public int Radius { get; set; }

    public void Draw()
    {
        Console.WriteLine($"Drawing a {Color} circle with radius {Radius}.");
    }

    public IShape Clone()
    {
        return new Circle { Color = this.Color, Radius = this.Radius };
    }
}

public class Square : IShape
{
    public string Color { get; set; }
    public int SideLength { get; set; }

    public void Draw()
    {
        Console.WriteLine($"Drawing a {Color} square with side length {SideLength}.");
    }

    public IShape Clone()
    {
        return new Square { Color = this.Color, SideLength = this.SideLength };
    }
}

// Client code
class Program
{
    static void Main(string[] args)
    {
        // Create a circle prototype
        Circle circlePrototype = new Circle { Color = "Red", Radius = 5 };

        // Clone the circle prototype and customize it
        Circle clonedCircle = (Circle)circlePrototype.Clone();
        clonedCircle.Color = "Blue";

        // Create a square prototype
        Square squarePrototype = new Square { Color = "Green", SideLength = 10 };

        // Clone the square prototype and customize it
        Square clonedSquare = (Square)squarePrototype.Clone();
        clonedSquare.SideLength = 8;

        // Draw the shapes
        circlePrototype.Draw();    // Output: Drawing a Red circle with radius 5.
        clonedCircle.Draw();       // Output: Drawing a Blue circle with radius 5.
        squarePrototype.Draw();    // Output: Drawing a Green square with side length 10.
        clonedSquare.Draw();       // Output: Drawing a Green square with side length 8.
    }
}

In this example, we have an interface IShape that defines the Draw method for rendering shapes and the Clone method for creating shape clones. The concrete shape classes (Circle and Square) implement this interface and provide their own implementations of the methods.

In the Main method, we create a prototype circle object (circlePrototype) with a red color and a radius of 5. We then clone this prototype to create a new circle object (clonedCircle) and change its color to blue.

Similarly, we create a prototype square object (squarePrototype) with a green color and a side length of 10. We clone it to create a new square object (clonedSquare) and modify its side length to 8.

Finally, we call the Draw method on each shape object to demonstrate that we have separate instances with their respective properties.

In this scenario, the Prototype pattern allows the user to create and modify shapes efficiently by cloning existing prototypes. It avoids the need to create new objects from scratch, thus improving performance and providing flexibility for customization.