-
Prototype Pattern
-
The Prototype Design Pattern is a creational design pattern that allows you to create new objects by duplicating (cloning) existing objects instead of building them from scratch. Rather than calling a constructor and reinitializing every attribute manually, the Prototype pattern gives you a standardized cloning mechanism that copies an object with all its internal values and configurations.
Think of it like: “Copy-Paste an object with all its properties instead of rebuilding it.”
This is especially useful when:- The object creation is costly (CPU heavy, time-consuming).
- The object has many complex properties.
- You need multiple copies with only small changes.
Why Do We Use the Prototype Pattern?
When object creation is expensive
Example: Loading a complex 3D model in a game takes time. Instead of reloading it repeatedly, you clone the already-loaded one.When the object has many configurations
Example: A form template for different users with only minor differences.When you want to hide the construction logic
Example: If creating the object requires many steps, prototypes simplify it.
In this prototype pattern, every prototype object exposes a dedicated
Clone()method(you can name it anything as per the context of class), which is responsible for copying the object’s internal state and returning a new, independent instance.This clone method is mandatory, because it is the core mechanism that makes the Prototype Pattern possible.
Prototype pattern Example:
Step 1: Define a Prototype Interface
Step 2: Create a Concrete Classpublic interface IPrototype<T> { T Clone(); }
Step 3: Using the Prototypepublic class Employee : IPrototype<Employee> { public string Name { get; set; } public string Department { get; set; } public Address EmpAddress { get; set; } public Employee Clone() { // Deep Copy Example return new Employee { Name = this.Name, Department = this.Department, EmpAddress = new Address { City = this.EmpAddress.City, Street = this.EmpAddress.Street } }; } } public class Address { public string City { get; set; } public string Street { get; set; } }Employee emp1 = new Employee { Name = "John", Department = "IT", EmpAddress = new Address { City = "New York" } }; Employee emp2 = emp1.Clone(); emp2.Name = "Johnny"; emp2.EmpAddress.City = "Los Angeles"; Console.WriteLine(emp1.EmpAddress.City); // New York Console.WriteLine(emp2.EmpAddress.City); // Los AngelesEach employee becomes an independent clone, not sharing the same memory references.
The cloning process may create:- a shallow copy (copy basic fields, share references)
- or a deep copy (duplicate the full object graph, including the nested objects)
Summary
The Prototype Design Pattern is a creational pattern that creates new objects by cloning an existing prototype instead of building them from scratch. It relies on a required Clone method, which defines how an object copies its internal state.
This makes the pattern ideal for situations where object creation is expensive, involves multiple configurations, or needs to happen repeatedly with slight variations. By enabling quick duplication and reducing complex initialization code, the Prototype Pattern improves performance, enhances flexibility, and simplifies the overall object creation process.