Prototype Pattern


  • The Prototype pattern is used to create new objects by cloning one of a few stored prototypes.
  • The Prototype pattern ensures that when copies of complex objects are made, they are true copies.
  • Given a key, the program creates an object of the required type, not by instantiation, but by copying a clean instance of the class. The copies, or clones, are objects in their own right, and the intention of the pattern is that their state can be altered at will without affecting the prototype.
  • Designs that make heavy use of the Composite and Decorator patterns often can benefit from the Prototype pattern as well.
  • Prototypes are useful when object initialization is expensive and you anticipate few variations on the initialization parameters.

Class diagram: (see Prototype Pattern code review)

Prototype Pattern class diagram of sample code download


Use the Prototype pattern when:

  • Hide concrete classes from the client.
  • Add and remove new classes (via prototypes) at runtime.
  • Keep the number of classes in the system to a minimum.
  • Adapt to changing structures of data at runtime.