Rules of Thumb
Sometimes creational patterns overlap - 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.
Read more about this topic: Prototype Pattern
Famous quotes containing the words rules of, rules and/or thumb:
“... geometry became a symbol for human relations, except that it was better, because in geometry things never go bad. If certain things occur, if certain lines meet, an angle is born. You cannot fail. Its not going to fail; it is eternal. I found in rules of mathematics a peace and a trust that I could not place in human beings. This sublimation was total and remained total. Thus, Im able to avoid or manipulate or process pain.”
—Louise Bourgeois (b. 1911)
“When I hear the hypercritical quarreling about grammar and style, the position of the particles, etc., etc., stretching or contracting every speaker to certain rules of theirs ... I see that they forget that the first requisite and rule is that expression shall be vital and natural, as much as the voice of a brute or an interjection: first of all, mother tongue; and last of all, artificial or father tongue. Essentially your truest poetic sentence is as free and lawless as a lambs bleat.”
—Henry David Thoreau (18171862)
“Your small hands, precisely equal to my own
only the thumb is larger, longerin these hands
I could trust the world, or in many hands like these,
handling power-tools or steering-wheel
or touching a human face ...”
—Adrienne Rich (b. 1929)