Tutorials Design Patterns Mastery

Prototype Pattern: Cloning high-cost objects

On this page

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."

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Design Patterns Mastery
Course syllabus
1. Introduction to Design Patterns
2. Creational Patterns
3. Structural Patterns
4. Behavioral Patterns
5. Modern Enterprise & Cloud Patterns
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details