SOLID & Design Patterns Interview Question #34
What is the Prototype Pattern?
Structural, Creational & Behavioral Patterns Junior Beginner
Quick Interview Answer
The Prototype Pattern creates new objects by copying an existing prototype rather than constructing each object from scratch. It is useful when initialization is expensive or when many objects share a configured starting state.
Detailed Explanation
The Prototype Pattern creates new objects by copying an existing prototype rather than constructing each object from scratch. It is useful when initialization is expensive or when many objects share a configured starting state.
The important design issue is copy semantics. A shallow copy may share referenced mutable objects, while a deep copy duplicates the required object graph.
Code Example
public record ReportTemplate(
string Title,
string Format,
bool IncludeCharts);
var standard = new ReportTemplate(
"Monthly Report", "PDF", true);
var custom = standard with
{
Title = "Customer Report"
};