Design Patterns Mastery

Prototype Pattern: Cloning high-cost objects

1 Views Updated 5/4/2026

The Prototype Pattern

The Prototype Pattern is used when the cost of creating a brand-new object from scratch is too high (e.g., it requires a database query or a 3-second network call). Instead of doing the work again, you simply "Clone" an existing object that you've already created.

1. Deep Clone vs Shallow Clone

If you object contains a list of other objects, a Shallow Clone just copies the memory address (both objects share the list). A Deep Clone copies everything, including the nested items.

public class User : ICloneable 
{
    public string Name { get; set; }
    
    public object Clone() 
    {
        // MemberwiseClone is a Shallow Clone built into .NET
        return (User)this.MemberwiseClone(); 
    }
}

2. Implementation Tip

In modern C#, you should avoid ICloneable (as it returns object and requires casting). Instead, define a strongly-typed Clone() method or use a Copy Constructor.

4. Interview Mastery

Q: "How does the C# 'Record' type implement the Prototype pattern?"

Architect Answer: "The `with` keyword in C# Records is a specialized compiler-implemented version of the Prototype pattern. When you write `var newUser = oldUser with { Age = 30 };`, the compiler internally clones the record (Prototype) and only changes the specified property. This ensures absolute immutability while making 'Cloning' feel like a first-class language feature."

Design Patterns Mastery
1. Introduction to Design Patterns
Introduction to GoF Patterns: Why patterns matter? SOLID Principles: The foundation of all patterns DRY, KISS, and YAGNI (Architectural Philosophy)
2. Creational Patterns
Singleton Pattern: Thread-safety & Captive Dependencies Factory Method: Abstracting complex object creation Abstract Factory: Creating families of related objects Builder Pattern: Constructing complex fluents Prototype Pattern: Cloning high-cost objects
3. Structural Patterns
Adapter Pattern: Bridging incompatible interfaces Bridge Pattern: Decoupling abstraction from implementation Composite Pattern: Managing tree structures & hierarchies Decorator Pattern: Enhancing behavior without inheritance Facade Pattern: Simplifying complex library subsystems Flyweight Pattern: Drastically reducing memory footprint Proxy Pattern: Interception, Lazy-Loading, and Security
4. Behavioral Patterns
Chain of Responsibility: Middleware & Pipeline Architecture Command Pattern: Implementing Undo/Redo & Queueing Interpreter Pattern: Building domain-specific languages Iterator Pattern: Unified traversal of collections Mediator Pattern: Decoupling components with MediatR Memento Pattern: Capturing and restoring object state Observer Pattern: Pub/Sub & Event-driven architecture State Pattern: Managing complex object lifecycles Strategy Pattern: Swappable algorithms at runtime Template Method: Defining skeleton algorithms Visitor Pattern: Separating operations from data structures
5. Modern Enterprise & Cloud Patterns
Repository & Unit of Work (The EF Core Standard) CQRS Pattern (Command Query Responsibility Segregation) Circuit Breaker & Retry Patterns (Resilience with Polly) Dependency Injection Pattern (The Modern King)