Skip to content

Latest commit

 

History

History
109 lines (79 loc) · 3.56 KB

File metadata and controls

109 lines (79 loc) · 3.56 KB

Prototype

Simple Explanation

The Prototype design pattern is a creational design pattern that allows an object to create a copy of itself by implementing a cloning mechanism. This pattern is useful when creating a new instance of a class is expensive or complex, and you want to avoid the overhead of creating new objects from scratch.

Deep Explanation

The Prototype pattern involves two main components:

  1. Prototype - an interface or abstract class that declares a method for cloning itself.

  2. Concrete Prototype - a class that implements the Prototype interface, providing its own implementation of the cloning method.

By implementing the Prototype pattern, you can create new objects by cloning existing objects, which can be more efficient and less complex than creating objects from scratch, especially when their construction is expensive or complicated. This pattern can also be used to create objects with a specific state, based on an existing object's state.

Examples

Let's imagine we have a simple application that creates and processes shapes with different configurations.

  1. Create the Prototype interface:
public interface IShape : ICloneable
{
    void Draw();
}
  1. Implement Concrete Prototype classes:
public class Circle : IShape
{
    public int Radius { get; set; }

    public Circle(int radius)
    {
        Radius = radius;
    }

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

    public object Clone()
    {
        return MemberwiseClone();
    }
}

public class Rectangle : IShape
{
    public int Width { get; set; }
    public int Height { get; set; }

    public Rectangle(int width, int height)
    {
        Width = width;
        Height = height;
    }

    public void Draw()
    {
        Console.WriteLine($"Drawing a rectangle with width {Width} and height {Height}.");
    }

    public object Clone()
    {
        return MemberwiseClone();
    }
}
  1. Use the Prototype pattern:
class Program
{
    static void Main(string[] args)
    {
        Circle circle = new Circle(10);
        circle.Draw();

        Circle clonedCircle = (Circle)circle.Clone();
        clonedCircle.Radius = 15;
        clonedCircle.Draw();

        Rectangle rectangle = new Rectangle(5, 7);
        rectangle.Draw();

        Rectangle clonedRectangle = (Rectangle)rectangle.Clone();
        clonedRectangle.Width = 8;
        clonedRectangle.Height = 10;
        clonedRectangle.Draw();
    }
}

In this example, IShape is the Prototype interface, and Circle and Rectangle are Concrete Prototype classes. The IShape interface inherits from the ICloneable interface, which requires implementing the Clone method for cloning objects.

The Circle and Rectangle classes implement the IShape interface and provide their own implementation of the Clone method using the MemberwiseClone method, which creates a shallow copy of the object. The Draw method demonstrates the shape's specific behavior.

The Main method demonstrates how the Prototype pattern can be used to create new shapes by cloning existing shapes and modifying their properties. By using the Prototype pattern, you can create new objects based on existing objects' state and behavior, avoiding the overhead of creating objects from scratch.

Note that the provided example uses shallow copying with the MemberwiseClone method. Depending on your use case and the complexity of your objects, you might need to implement deep copying to create completely independent copies of your objects.