Prototype Pattern

Allows making new instances by copying an existing instance ! Cheap Objects

  • Allows making new instances by copying an existing instance.
  • It is a clone object that is different from original
  • Template of an object
  • Minimum instance creation process
  • Initially object has same state but can change after cloning or prototyping.
  • Client can make new instances without knowing which specific class is initialized.
  • Creation is through delegation.
  • Prototype object has same properties initially
  • Mandates that object have copying properties i.e. shallow or deep copy.
  • Hides complexity of creating objects
  • Reduces sub-classing.
  • Cons – making a copy is sometimes complicates in case of complex objects – Class with circular reference difficult to clone
  • Can co-exists with Factory & singleton pattern
Applicability

An object is loaded from database & needs to be modified multiple times with different values. So rather them loading object from DB again & again, clone the object & play with it.

Used When
  • Multiple similar objects are used.
  • Removing or adding objects at runtime
  • Cost of creating an object is expensive or complex

ARCHITECTURES · DESIGN-PATTERNS
architectures design-patterns prototyping prototype

You May Also Enjoy

ELK Stack!

Prototype Pattern

Liskov Substitution Principle

Prototype Pattern

Static Class Vs SingleTon Pattern

Prototype Pattern

Layer Wise Design Patterns

Prototype Pattern


Dialogue & Discussion