0
0
LLDsystem_design~7 mins

Prototype pattern in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
Creating new objects by initializing them from scratch can be slow and error-prone, especially when many objects share similar properties. This leads to duplicated code and inconsistent object states when copying manually.
Solution
The Prototype pattern solves this by cloning existing objects to create new ones. Instead of building from zero, it copies a prototype object and then modifies it if needed, ensuring consistency and saving time.
Architecture
Client
Prototype
Cloned Object
Cloned Object

This diagram shows the client requesting a clone from the prototype object, which returns a new cloned object.

Trade-offs
✓ Pros
Speeds up object creation by copying existing instances.
Ensures new objects start with a consistent state.
Reduces subclassing by cloning different prototypes.
Simplifies object creation when constructors are complex.
✗ Cons
Cloning complex objects with deep references can be tricky and error-prone.
Requires careful implementation of clone methods to avoid shared mutable state.
May increase memory usage if many prototypes are kept in memory.
Use when object creation is costly or complex, and many similar objects are needed quickly, especially if they differ only slightly.
Avoid when objects are simple to create or when cloning deep object graphs is too complex or risky.
Real World Examples
Adobe
Uses prototype pattern in Photoshop to quickly duplicate complex image objects with many properties.
Unity
Game engine uses prototype pattern to clone game objects during runtime for performance and flexibility.
Google
In Google Docs, prototype pattern helps duplicate document elements efficiently while preserving formatting.
Code Example
Before applying the Prototype pattern, copying an object requires manually creating a new instance and copying each attribute, which is error-prone and verbose. After applying the pattern, the clone method uses deep copy to create a new object with the same state, simplifying object creation and ensuring consistency.
LLD
### Before Prototype Pattern (manual copy)
class Car:
    def __init__(self, model, color, options):
        self.model = model
        self.color = color
        self.options = options

car1 = Car('Sedan', 'Red', ['GPS', 'Sunroof'])

# Manual copy
car2 = Car(car1.model, car1.color, list(car1.options))

### After Prototype Pattern (using clone method)
import copy

class CarPrototype:
    def __init__(self, model, color, options):
        self.model = model
        self.color = color
        self.options = options

    def clone(self):
        return copy.deepcopy(self)

car1 = CarPrototype('Sedan', 'Red', ['GPS', 'Sunroof'])
car2 = car1.clone()
OutputSuccess
Alternatives
Factory Method
Creates objects by calling a factory method instead of cloning existing instances.
Use when: Use when object creation logic varies by subclass and cloning is not suitable.
Builder
Constructs complex objects step-by-step rather than cloning a prototype.
Use when: Use when object construction requires multiple steps or configurations.
Summary
Prototype pattern creates new objects by cloning existing ones to save time and ensure consistency.
It is useful when object creation is costly or complex and many similar objects are needed.
Careful implementation is required to handle deep copying and avoid shared mutable state.