Skip to content

Commit

Permalink
add readme for prototype pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
saadati944 committed May 4, 2021
1 parent b250c19 commit 54e5d3b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ Also if you know another algorithm or data structure that was not in here, creat
- [Creational patterns](./design_patterns/creational_patterns)
- [Abstract factory pattern](./design_patterns/creational_patterns/abstract_factory_pattern)
- [Builder pattern](./design_patterns/creational_patterns/builder_pattern)
- [Builder pattern](./design_patterns/creational_patterns/factory_method)
- [Factory pattern](./design_patterns/creational_patterns/factory_method)
- [Prototype](./design_patterns/creational_patterns/prototype)

# Contributing

Expand Down
16 changes: 16 additions & 0 deletions design_patterns/creational_patterns/prototype/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Prototype

![prototype](https://upload.wikimedia.org/wikipedia/commons/1/14/Prototype_UML.svg)

[read more in wikipedia](https://en.wikipedia.org/wiki/Prototype_pattern)

The prototype pattern is a creational design pattern in software development. It is used when the type 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.
- 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, declare 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.

0 comments on commit 54e5d3b

Please sign in to comment.