Prototype Pattern in C#: Definition, Examples, Pros & Cons
The Prototype pattern in C# creates new objects by cloning an existing instance instead of instantiating new ones from scratch.
The Prototype pattern is a creational design pattern that relies on copying existing objects to create new ones. Instead of constructing objects through constructors, it uses a cloning mechanism to duplicate an already configured instance. This is especially useful when object creation is costly or complex. In C#, cloning is often implemented using the ICloneable interface or custom deep copy logic. The pattern supports both shallow and deep copies depending on how object data is duplicated.
Why We Use Prototype Pattern in C#?
We use the Prototype pattern to improve performance when object creation is expensive or resource-intensive. It allows reuse of existing configurations instead of rebuilding objects from scratch. It also simplifies object creation when initialization logic is complex or repetitive.
When to Use Prototype Pattern in C#?
Use this pattern when creating objects is costly (e.g., database calls, heavy computations). It is useful when many similar objects are needed with slight differences. It also fits scenarios where object configuration should be copied rather than recreated. If your system needs to dynamically create objects at runtime without tight coupling to classes, Prototype is a good choice.
Example Use Cases with C# Code
1. Basic Prototype with ICloneable (Shallow Copy)
using System;
public class Person : ICloneable
{
public string Name { get; set; }
public int Age { get; set; }
public object Clone()
{
return this.MemberwiseClone(); // Shallow copy
}
}
// Usage
var p1 = new Person { Name = "Alice", Age = 30 };
var p2 = (Person)p1.Clone();
p2.Name = "Bob";
Console.WriteLine(p1.Name); // Alice
Console.WriteLine(p2.Name); // Bob
2. Deep Copy Example
using System;
public class Address
{
public string City { get; set; }
}
public class Employee : ICloneable
{
public string Name { get; set; }
public Address Address { get; set; }
public object Clone()
{
return new Employee
{
Name = this.Name,
Address = new Address { City = this.Address.City } // Deep copy
};
}
}
// Usage
var emp1 = new Employee
{
Name = "John",
Address = new Address { City = "Istanbul" }
};
var emp2 = (Employee)emp1.Clone();
emp2.Address.City = "Ankara";
Console.WriteLine(emp1.Address.City); // Istanbul
Console.WriteLine(emp2.Address.City); // Ankara
3. Prototype Registry (Caching Prototypes)
using System;
using System.Collections.Generic;
public abstract class Shape
{
public string Color { get; set; }
public abstract Shape Clone();
}
public class Circle : Shape
{
public override Shape Clone()
{
return (Shape)this.MemberwiseClone();
}
}
public class ShapeRegistry
{
private readonly Dictionary<string, Shape> _shapes = new();
public void Add(string key, Shape shape)
{
_shapes[key] = shape;
}
public Shape Get(string key)
{
return _shapes[key].Clone();
}
}
// Usage
var registry = new ShapeRegistry();
registry.Add("redCircle", new Circle { Color = "Red" });
var cloned = registry.Get("redCircle");
Console.WriteLine(cloned.Color); // Red
Advantages of Prototype Pattern in C#
• Improves performance by avoiding costly object creation
• Reduces need for complex initialization logic
• Supports dynamic object creation at runtime
• Simplifies creation of similar objects
• Can reduce subclassing
Disadvantages of Prototype Pattern in C#
• Deep cloning can be complex and error-prone
• Requires careful handling of object references
• ICloneable is not always recommended due to ambiguity
• Cloning large object graphs can impact performance
• May introduce hidden bugs if cloning is not implemented correctly
Comparison with Similar Patterns
| Pattern | Purpose | Key Difference | Use Case |
|---|---|---|---|
| Prototype | Clone existing objects | Uses copying instead of instantiation | Performance-critical object creation |
| Factory Method | Create a single object | Uses subclasses for instantiation | Flexible object creation |
| Abstract Factory | Create families of objects | Produces related object groups | UI systems, product families |
| Builder | Construct complex objects step by step | Focuses on construction process | Complex configurations |