Html/Javascript widget

Saturday, 27 August 2022

Prototype

The prototype pattern is a creational design pattern Builder, Abstract factory, Factory method, Prototype, Singleton used when the type of objects to create is determined by a prototypical instance, cloned to produce new objects. This pattern is used to avoid subclasses of an object creator in the client application, like the factory method pattern does and to avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is prohibitively expensive for a given application.

To implement the pattern, the client declares an abstract base class that specifies a pure virtual clone() method. Any class that needs a "polymorphic constructor" capability derives itself from the abstract base class, and implements the clone() operation.

The mitotic division of a cell - resulting in two identical cells - is an example of a prototype that plays an active role in copying itself and thus, demonstrates the Prototype pattern. When a cell splits, two cells of identical genotype result. In other words, the cell clones itself.

Overview

    The prototype design pattern solves problems like:
  • How can objects be created so that which objects to create can be specified at run-time?
  • How can dynamically loaded classes be instantiated?

Creating objects directly within the class that requires the objects is inflexible because it commits the class to particular objects at compile-time and makes it impossible to specify which objects to create at run-time.

    The prototype design pattern describes how to solve such problems:
  1. Define a Prototype object that returns a copy of itself.
  2. Create new objects by copying a Prototype object.

This enables configuration of a class with different Prototype objects, which are copied to create new objects. Prototype objects can be added and removed at run-time.

No comments:

Post a Comment