Prototype pattern

Prototype pattern

The prototype pattern is a creational design pattern used in software development 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 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.



UML class diagram describing the Prototype design pattern


The Prototype pattern 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 - 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.[1]


 * Prototype class
abstract class Prototype implements Cloneable {
        public Object clone() throws CloneNotSupportedException {
                return super.clone();
        public abstract void setX(int x);
        public abstract void printX();
        public abstract int getX();
 * Implementation of prototype class
class PrototypeImpl extends Prototype {
        int x;
        public PrototypeImpl(int x) {
                this.x = x;
        public void setX(int x) {
                this.x = x;
        public void printX() {
                System.out.println("Value :" + x);
        public int getX() {
                return x;
 * Client code
public class PrototypeTest {
        public static void main(String args[]) throws CloneNotSupportedException {
                Prototype prototype = new PrototypeImpl(1000);
                for (int i=1;i<10;i++) {
                        Prototype tempotype = (Prototype) prototype.clone();
                        // Usage of values in prototype to derive a new value.
                        tempotype.setX( tempotype.getX() * i);
**Code output**
Value :1000
Value :2000
Value :3000
Value :4000
Value :5000
Value :6000
Value :7000
Value :8000
Value :9000

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 which 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 which 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.

See also


  1. ^ Michael Duell, "Non-software examples of software design patterns", Object Magazine, Jul 97, p. 54

Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Prototype (disambiguation) — A prototype is something that is representative of a category of things. Prototype may also refer to:;Automobiles* Citroën Prototype C, range of vehicles created by Citroën from 1955 to 1956 * Citroën Prototype Y, project of replacement of the… …   Wikipedia

  • Prototype (Entwurfsmuster) — Ein Prototyp (engl. Prototype) ist ein Entwurfsmuster (design pattern) aus dem Bereich der Softwareentwicklung und gehört zur Kategorie der Erzeugungsmuster (Creational Patterns). Neue Instanzen werden aufgrund prototypischer Instanzen… …   Deutsch Wikipedia

  • Pattern recognition (psychology) — Pattern recognition involves identification of faces, objects, words, melodies, etc. The visual system does more than just interpret forms, contours and colors. Pattern recognition refers to the process of recognizing a set of stimuli arranged in …   Wikipedia

  • pattern — pat·tern / pa tərn/ n 1: a form or model proposed for imitation 2: a recognizably consistent series of related acts found a pattern of discrimination in that company a pattern of racketeering activity Merriam Webster’s Dictionary of Law. Merriam… …   Law dictionary

  • Prototype (Motif de conception) — Prototype (patron de conception) Pour les articles homonymes, voir Prototype. Le patron de conception prototype est utilisé lorsque la création d une instance est complexe ou consommatrice en temps. Plutôt que créer plusieurs instances de la… …   Wikipédia en Français

  • Prototype (motif de conception) — Prototype (patron de conception) Pour les articles homonymes, voir Prototype. Le patron de conception prototype est utilisé lorsque la création d une instance est complexe ou consommatrice en temps. Plutôt que créer plusieurs instances de la… …   Wikipédia en Français

  • prototype — I noun archetype, example, exemplar, exemplum, first, guide, ideal, model, mold, original, paradigm, paragon, pattern, precedent, protoplast, sample, source, standard II index constant, exemplar, model …   Law dictionary

  • pattern — [n1] design, motif arrangement, decoration, device, diagram, figure, guide, impression, instruction, markings, mold, motive, original, ornament, patterning, plan, stencil, template, trim; concepts 259,625 Ant. plainness pattern [n2] arrangement,… …   New thesaurus

  • Prototype — Pro to*type, n. [F., from L. prototypus original, primitive, Gr. ?, ?; ? first + ? type, model. See {Proto }, and {Type}] An original or model after which anything is copied; the pattern of anything to be engraved, or otherwise copied, cast, or… …   The Collaborative International Dictionary of English

  • prototype — [n] original, example ancestor, antecedent, antecessor, archetype, criterion, first, forerunner, ideal, mock up*, model, norm, paradigm, pattern, precedent, precursor, predecessor, standard, type; concept 686 …   New thesaurus

We are using cookies for the best presentation of our site. Continuing to use this site, you agree with this.