Prototype pattern

A prototype (English prototype) is a design pattern ( design pattern ) from the field of software development and belongs to the category of production patterns ( creational patterns ). New instances are generated on the basis of prototypical instances ('templates' ). The document is copied and adapted to new needs. The pattern is one of the so-called GoF patterns ( Gang of Four, see Gang of Four ).


A prototype is used when

  • The generation of further instances of a class -consuming (expensive ) and are similar objects,
  • To instantiate the classes are known only at runtime,
  • A hierarchy of plants is to be avoided in parallel to a hierarchy of products, or
  • The objects of a class can have only a few combinations of states; or
  • The template editing is very similar or identical to the objects.

UML diagram

The following class diagram shows the roles involved in the design pattern. A client calls the method clone () an object of type prototype and get either an object of type KonkreterPrototyp1 or type KonkreterPrototyp2 back, depending on which of the two types is behind the interface prototype in this simple association (UML).


The type prototype defines an interface to copy an object. Objects of type KonkreterPrototyp1 or KonkreterPrototyp2 copy itself by calling its implementation of the prescribed by the interface clone ( ) method. We distinguish between shallow copy (shallow cloning ) and deep copy (deep cloning ). The former can be references made ​​to other objects, the latter more also copied the referenced objects. Usually, a deep copy of a prototype is made (except for non- modifiable objects ), but the exact details have to be clarified on a case by case basis.

The client creates new objects as copies of existing objects and modify them.

Note: The use of a clone ( ) method is not equivalent to the application of a prototype pattern. In the prototype pattern is conceptually distinguish objects between the prototype and the (useful), so there is not any object ( a class hierarchy ) as a prototype. Therefore, when copying the prototype for the purpose of object creation is also at least the "Status = prototype " flag (or similar) to be changed, and therefore is no longer true that the prototype is equal to the object created. It makes sense to own Instanzierungsmethode or a clone ( ) method with parameter is used for this purpose.


Complex objects can be generated faster. New subclasses can be included at runtime. New objects can be specified by varying the structure. There is no generator class hierarchy parallel to the class hierarchy of products.


Creating a copy of an object can be costly. Each subclass must implement the copy operation. Any initializations of the copied object must be additional.

Use in document processing

In the document processing (eg Microsoft Office, LibreOffice ) is the prototype patterns Standard: The templates are processed with the same tools as the actual documents; to create a document template is copied and then further processed. Often there is a default template (eg " "), which can serve as a minimal prototype for objects or other templates.


In JDK the prototype pattern is not used explicitly; only in the javax.swing.text.EditorKit class is argued that new instances are to be created by cloning another instance. Many classes in the JDK, however, implement a public clone () method, which can in principle be used as the basis of a prototype patterns.

Implementation in C #

In. NET base class System.Object provides the protected (protected) method Static (), which creates a shallow copy of the object, ie worth -prone types ( int, decimal, etc.) are byte-wise copied 1 to 1, with references, only the reference pointer is copied, not the object itself, which is indicated by the new reference. To make the pattern for a client to use, the prototype nor the ICloneable interface must implement the clone () method:

Using System; using System.Text;   namespace Prototype {      abstract class Prototype Of T where T: Prototype      {          public T Clone ()          {              return ( T) this.MemberwiseClone ();          }            public abstract void print ();          public abstract int X {get; set; }      }        ConcretePrototype class: Prototype      {          public ConcretePrototype (int x ) { X = x; }          public override void Print ()          {              Console.WriteLine (X);          }            public override int X {get; set; }      }        class client      {          static void Main ( string [ ] args)          {              int num = 1000;              ConcretePrototype tempProt = null;              ConcretePrototype prot = new ConcretePrototype (num);                for (int i = 0; i < 10; i )              {                  tempProt = prot.Clone ();                  tempProt.X * = i;                  tempProt.Print ();              }          }      } } Related design patterns

A hand abstract factory and prototype compete with each other because they may generate both different types of objects. On the other hand, they can be combined with each other, if an abstract factory prototypes produced, which can then be cloned without the aid of a factory.

Composite and Decorator are often used together with prototypes.