FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Prototype_pattern

Prototype pattern

(Learn how and when to remove this message)

The prototype pattern is a creational design pattern in software development. It is used when the types of objects to create is determined by a prototypical instance, which is 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 client, instead of writing code that invokes the "new" operator on a hard-coded class name, calls the clone() method on the prototype, calls a factory method with a parameter designating the particular concrete derived class desired, or invokes the clone() method through some mechanism provided by another design pattern.

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.

Abstract factory classes are often implemented with factory methods (creation through inheritance), but they can be implemented using prototype (creation through delegation).

Designs that make heavy use of the composite and decorator patterns often can benefit from Prototype as well.Maze::addRoom0x1160f50 Maze::addRoom0x1160f70 Room::setSide00x11613c0 Room::setSide20x1160f90 Room::setSide10x11613e0 Room::setSide30x1161400 Room::setSide00x1161420 Room::setSide20x1161440 Room::setSide10x1161460 Room::setSide30x1160f90

See also

edit
The Wikibook Computer Science Design Patterns has a page on the topic of: Prototype implementations in various languages

References

edit
<a href=\"./Prototype_pattern#cite_note-GoF-2\" id=\"mwBzA\"><span class=\"mw-reflink-text\" id=\"mwBzE\"><span class=\"cite-bracket\" id=\"mwBzI\">[</span>2<span class=\"cite-bracket\" id=\"mwBzM\">]</span></span></a></sup>\n"}}'>
  1. ↑ "The Prototype design pattern - Problem, Solution, and Applicability". w3sDesign.com. Retrieved 2017-08-17.
Software design patterns
Gang of Four
patterns
Creational
Structural
Behavioral
Concurrency
patterns
Architectural
patterns
Other
patterns
Books
People
Communities
See also

View original on Wikipedia | Back | FazBrowse Home