Design Patterns Explained
  • Getting Started
  • SOLID Principles
    • Single Responsibility Principle
    • Open / Closed Principle
    • Liskov Substitution Principle
    • Interface Segregation Principle
    • Dependency Inversion Principle
  • Design Patterns
    • Creational Patterns
      • Abstract Factory Pattern
      • Builder Pattern
      • Factory Pattern
      • Prototype Pattern
      • Singleton Pattern
    • Behavioral Patterns
      • Chain Of Responsibility Pattern
      • Command Pattern
      • Interpreter Pattern
      • Iterator Pattern
      • Mediator Pattern
        • Example 1
      • Memento Pattern
      • Observer Pattern
      • State Pattern
      • Strategy Pattern
        • Example 1
      • Template Method Pattern
      • Visitor Pattern
    • Structural Patterns
      • Adapter Pattern
      • Bridge Pattern
      • Composite Pattern
      • Decorator Pattern
      • Facade Pattern
      • Flyweight Pattern
      • Proxy Pattern
  • Roadmap
Powered by GitBook
On this page
  • Intent
  • When to use
  • Structure
  • Examples

Was this helpful?

  1. Design Patterns
  2. Creational Patterns

Prototype Pattern

PreviousFactory PatternNextSingleton Pattern

Last updated 4 years ago

Was this helpful?

Intent

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype instead of depending on the new operator.

When to use

  1. When the object instantiation from the beginning takes a lot of time.

  2. When we want to specify an instance of the class to use it as a breeder of all future instances.

  3. To avoid building a class hierarchy (seperate classes or factories) that parallels the class hierarchy of the products.

  4. If we want to instantiate the same class with a minimal change of state, it's more appropriate to clone the instance and change its state to the required state.

Structure

  • Prototype: defines the interface for cloning

  • ConcretePrototype: implements the operation for cloning itself.

  • Client: creates a new object by asking a prototype to clone itself.

  • Registry: optional class that saves the frequently used prototypes, either before compilation, or even in runtime.

Examples

Source Code

UML

// TODO

// TODO

You can find the tests .

here
Example 1
Example 2