Computer Science Design Patterns/Prototype

{{Computer Science Design Patterns/Page The prototype pattern 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:
 * mode={{{mode|reading}}}
 * page={{{page|{{SUBPAGENAME}}}}}
 * stage=25
 * stage date=21 Jun, 2013
 * stage comment=Add more examples of use.
 * previous=Observer
 * next=Proxy
 * content=
 * avoid subclasses of an object creator in the client application, like the abstract factory 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.

Rules of thumb
Sometimes creational patterns overlap &mdash; there are cases when either Prototype or Abstract Factory would be appropriate. At other times they complement each other: Abstract Factory might store a set of Prototypes from which to clone and return product objects (GoF, p126). Abstract Factory, Builder, and Prototype can use Singleton in their implementations. (GoF, p81, 134). Abstract Factory classes are often implemented with Factory Methods (creation through inheritance), but they can be implemented using Prototype (creation through delegation). (GoF, p95)

Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed. (GoF, p136)

Prototype doesn't require subclassing, but it does require an "initialize" operation. Factory Method requires subclassing, but doesn't require initialization. (GoF, p116)

Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well. (GoF, p126)

The rule of thumb could be that you would need to clone an Object when you want to create another Object at runtime that is a true copy of the Object you are cloning. True copy means all the attributes of the newly created Object should be the same as the Object you are cloning. If you could have instantiated the class by using new instead, you would get an Object with all attributes as their initial values. For example, if you are designing a system for performing bank account transactions, then you would want to make a copy of the Object that holds your account information, perform transactions on it, and then replace the original Object with the modified one. In such cases, you would want to use clone instead of new.

Advices

 * Put the prototype term in the name of the prototype classes to indicate the use of the pattern to the other developers.
 * Only use an interface when it is necessary.

Implementations
It specifies the kind of objects to create using a prototypical instance. Prototypes of new products are often built prior to full production, but in this example, the prototype is passive and does not participate in copying itself. The mitotic division of a cell &mdash; resulting in two identical cells &mdash; 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.

The concrete type of object is created from its prototype. MemberwiseClone is used in the Clone method to create and return a copy of ConcreteFoo1 or ConcreteFoo2.

Another example:

Code output:

Value :1000 Value :2000 Value :3000 Value :4000 Value :5000 Value :6000 Value :7000 Value :8000 Value :9000

This pattern creates the kind of object using its prototype. In other words, while creating the object of Prototype object, the class creates a clone of it and returns it as prototype. The clone method has been used to clone the prototype when required.

Another example:

Code output:

Value: 1000 Value: 2000 Value: 3000 Value: 4000 Value: 5000 Value: 6000 Value: 7000 Value: 8000 Value: 9000

Another example:

Let's write an occurrence browser class for a text. This class lists the occurrences of a word in a text. Such an object is expensive to create as the locations of the occurrences need an expensive process to find. So, to duplicate such an object, we use the prototype pattern:

class WordOccurrences is field occurrences is The list of the index of each occurrence of the word in the text. constructor WordOccurrences(text, word) is input: the text in which the occurrences have to be found input: the word that should appear in the text Empty the occurrences list for each textIndex in text isMatchingg:= true for each wordIndex in word if the current word character does not match the current text character then isMatchingg:= false if isMatching is true then Add the current textIndex into the occurrences list method getOneOccurrenceIndex(n) is input: a number to point on the nth occurrence. output: the index of the nth occurrence. Return the nth item of the occurrences field if any. method clone is output: a WordOccurrences object containing the same data. Call clone on the super class. On the returned object, set the occurrences field with the value of the local occurrences field. Return the cloned object. texte:= "The prototype pattern is a creational design pattern in software development first described in design patterns, the book." wordw:= "pattern"d searchEnginen:= new WordOccurrences(text, word) anotherSearchEngineE:= searchEngine.clone

(the search algorithm is not optimized; it is a basic algorithm to illustrate the pattern implementation)

This implementation uses the decorator pattern.

Output:

Creating A Cloning A to B A and B somevars 0 0 Changing A somevar to 10... A and B somevars 10 0 Creating another kind of class as C Cloning C to D Changing C somevar to 100 C and D somevars 100 50

}}