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:
- Define a Prototype object that returns a copy of itself.
- 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