Here, we will explain Prototype Design Pattern with example in C#
The Prototype design pattern is a creational pattern that allows you to create new objects by copying an existing object, known as the prototype. This pattern is particularly useful when object creation is costly, and you want to avoid creating objects from scratch. It provides an efficient way to create objects with existing states.
A real-world example of the prototype pattern is manufacturing. When a company designs a prototype for a new product (such as a car), the prototype is tested and refined until it meets the standards. Once the design is finalized, the company can create multiple identical units based on that prototype without going through the entire design process again.
For instance, when producing a car, the manufacturer creates a prototype. Once the prototype is approved, the same model is produced in bulk by cloning the prototype design.
using System;
// Prototype Interface
public interface ICarPrototype
{
ICarPrototype Clone();
}
// Concrete Prototype
public class Car : ICarPrototype
{
public string Model { get; set; }
public string Color { get; set; }
public string EngineType { get; set; }
public Car(string model, string color, string engineType)
{
Model = model;
Color = color;
EngineType = engineType;
}
// Clone method to create a copy of the car
public ICarPrototype Clone()
{
return (ICarPrototype)this.MemberwiseClone();
}
public void ShowDetails()
{
Console.WriteLine($"Car Model: {Model}, Color: {Color}, Engine: {EngineType}");
}
}
// Client
class Program
{
static void Main(string[] args)
{
// Create a prototype car
Car prototypeCar = new Car("Sedan", "Black", "V8");
// Clone the prototype to create new cars
Car car1 = (Car)prototypeCar.Clone();
car1.Color = "Red"; // Customize the cloned car
Car car2 = (Car)prototypeCar.Clone();
car2.Color = "Blue"; // Customize the cloned car
// Display details of the cloned cars
prototypeCar.ShowDetails(); // Prototype car
car1.ShowDetails(); // Customized car1
car2.ShowDetails(); // Customized car2
}
}
Clone()
method that will be implemented by all concrete classes.Car
class implements
ICarPrototype
and provides a cloning mechanism through the
Clone()
method, which uses the MemberwiseClone()
method
to create a shallow copy of the object.car1
and car2
),
which are customized with different colorsOutput
Imagine a graphical editing software where you have various shapes like circles, rectangles, and triangles. Each shape has different properties such as size, color, and position. If the user wants to create multiple copies of a shape, instead of recreating the shape from scratch with the same properties, the application can clone an existing shape (prototype) and modify it if necessary.
using System;
using System.Collections.Generic;
// Prototype Interface
public abstract class Shape
{
public int X { get; set; }
public int Y { get; set; }
public string Color { get; set; }
// Abstract method for cloning the shape
public abstract Shape Clone();
// Display method to show shape information
public virtual void Display()
{
Console.WriteLine($"Shape: {GetType().Name}, X: {X}, Y: {Y}, Color: {Color}");
}
}
// Concrete Prototype: Circle
public class Circle : Shape
{
public int Radius { get; set; }
// Clone method that copies the Circle object
public override Shape Clone()
{
return (Shape)MemberwiseClone();
}
// Display additional information about the circle
public override void Display()
{
Console.WriteLine($"Shape: Circle, X: {X}, Y: {Y}, Color: {Color}, Radius: {Radius}");
}
}
// Concrete Prototype: Rectangle
public class Rectangle : Shape
{
public int Width { get; set; }
public int Height { get; set; }
// Clone method that copies the Rectangle object
public override Shape Clone()
{
return (Shape)MemberwiseClone();
}
// Display additional information about the rectangle
public override void Display()
{
Console.WriteLine($"Shape: Rectangle, X: {X}, Y: {Y}, Color: {Color}, Width: {Width}, Height: {Height}");
}
}
// Concrete Prototype: Triangle
public class Triangle : Shape
{
public int Base { get; set; }
public int Height { get; set; }
// Clone method that copies the Triangle object
public override Shape Clone()
{
return (Shape)MemberwiseClone();
}
// Display additional information about the triangle
public override void Display()
{
Console.WriteLine($"Shape: Triangle, X: {X}, Y: {Y}, Color: {Color}, Base: {Base}, Height: {Height}");
}
}
// Shape Manager to handle prototypes
public class ShapeManager
{
private Dictionary<string, Shape> _shapes = new Dictionary<string, Shape>();
// Register a shape prototype
public void RegisterShape(string key, Shape shape)
{
_shapes[key] = shape;
}
// Get a cloned shape
public Shape GetShape(string key)
{
if (_shapes.ContainsKey(key))
{
return _shapes[key].Clone();
}
else
{
throw new ArgumentException("Shape not found");
}
}
}
public class Program
{
public static void Main()
{
// Create initial shape prototypes
Circle circle = new Circle { X = 10, Y = 20, Color = "Red", Radius = 15 };
Rectangle rectangle = new Rectangle { X = 30, Y = 40, Color = "Blue", Width = 100, Height = 50 };
Triangle triangle = new Triangle { X = 50, Y = 60, Color = "Green", Base = 70, Height = 35 };
// Shape Manager to store prototypes
ShapeManager shapeManager = new ShapeManager();
shapeManager.RegisterShape("circle", circle);
shapeManager.RegisterShape("rectangle", rectangle);
shapeManager.RegisterShape("triangle", triangle);
// Create clones of the shapes
Shape clonedCircle = shapeManager.GetShape("circle");
Shape clonedRectangle = shapeManager.GetShape("rectangle");
Shape clonedTriangle = shapeManager.GetShape("triangle");
// Modify the cloned shapes
clonedCircle.X = 100;
clonedCircle.Y = 200;
clonedCircle.Color = "Yellow";
clonedRectangle.X = 300;
clonedRectangle.Y = 400;
clonedRectangle.Color = "Purple";
clonedTriangle.X = 500;
clonedTriangle.Y = 600;
clonedTriangle.Color = "Orange";
// Display the original and cloned shapes
Console.WriteLine("Original Shapes:");
circle.Display();
rectangle.Display();
triangle.Display();
Console.WriteLine("\nCloned and Modified Shapes:");
clonedCircle.Display();
clonedRectangle.Display();
clonedTriangle.Display();
}
}
In the above example, the Circle object can be cloned, making it easy to create similar shapes without reinitializing them.
Shape Class (Prototype Interface) - This is the base
class that defines common properties like X
, Y
,
and Color
for all shapes. It includes the Clone()
method which will be overridden in the derived classes.
Concrete Prototype Classes (Circle, Rectangle, Triangle) -
These classes inherit from Shape
and implement the
Clone()
method using the MemberwiseClone()
method to
create shallow copies of objects.
ShapeManager Class - This class acts as a registry to
store the original prototypes and allows cloning of the stored shapes by
calling their Clone()
method.
Main Program
Outout