Prototype (design sample)

from Wikipedia, the free encyclopedia

A prototype ( Engl. Prototype ) is a design pattern ( design pattern ) in the field of software development and is part of the generation pattern (Engl. Creational patterns ). New instances are created on the basis of prototypical instances (“templates”). The template is copied and adapted to new requirements. The pattern is one of the so-called GoF patterns ( Gang of Four , see Gang of Four ).

use

A prototype is applied when

  • the creation of further instances of a class is complex (expensive) and the objects are similar,
  • the classes to be instantiated are only known at runtime,
  • a hierarchy of factories parallel to a hierarchy of products should be avoided, or,
  • the objects of a class can only assume a few combinations of states; or
  • the editing of templates is very similar or the same as that of objects

UML diagram

The following class diagram shows the roles involved in the design pattern. A Klientcalls the method of klone()an object of the type Prototypand receives either an object of the type KonkreterPrototyp1or of the type KonkreterPrototyp2, depending on which of the two types is hidden behind the interface Prototypin this simple association (UML) .

Class diagram showing the roles involved in the design pattern.

actors

The type Prototypdefines an interface for copying an object. Objects of type KonkreterPrototyp1or KonkreterPrototyp2copy themselves by calling their implementation of the klone()method prescribed by the interface . A distinction is made between shallow cloning and deep cloning . The former leaves references to other objects, the latter also copies the referenced objects. A deep copy of a prototype is usually made (except for non-modifiable objects ), but the exact details need to be clarified on a case-by-case basis.

The Klientcreates new objects as copies of existing objects and modifies them.

Note: Using a clone()method is not the same as applying a prototype pattern. In the prototype pattern, a conceptual distinction is made between the prototype and the (useful) objects , so not every object (a class hierarchy) serves as a prototype. Therefore, when copying the prototype for the purpose of generating the object, at least the "Status = Prototype" flag (or the like) should also be changed, and it is therefore no longer the case that the prototype is the same as the generated object. It makes sense to use a separate instantiation clone()method or a method with parameters for this.

advantages

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

disadvantage

Creating a copy of an object can be time-consuming. Each subclass must implement the copy operation. Any initializations of the copied object must also be carried out.

Use in document processing

The prototype pattern is standard in document processing (e.g. Microsoft Office, LibreOffice ): The templates are processed with the same tools as the actual documents; To create a document, a template is copied and then further processed. Often there is a standard template (e.g. "Normal.dot") that can serve as a minimal prototype for objects or other templates.

example

The prototype pattern is not used explicitly in the JDK ; only for the class javax.swing.text.EditorKitis it stated that new instances are to be created by cloning another instance. However, many classes in the JDK implement a public clone()-method that can in principle be used as the basis of a prototype pattern.

Implementation in C #

In .NET , the base class provides the System.Objectprotected method MemberwiseClone(), which creates a shallow copy of the object; H. value-prone types ( int, decimaletc.) are copied byte-1 to 1, with references is copied only the reference pointer and not the object itself, to the new reference shows. In order to make the pattern usable for a client, the prototype still has to implement the interface ICloneablewith the method clone():

using System;
using System.Text;

namespace Prototype
{
    abstract class Prototype<T> where T : Prototype<T>
    {
        public T Clone()
        {
            return (T)this.MemberwiseClone();
        }

        public abstract void Print();
        public abstract int X { get; set; }
    }

    class ConcretePrototype : Prototype<ConcretePrototype>
    {
        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

On the one hand, the abstract factory and the prototype compete with each other because they can both create different types of objects. On the other hand, they can be combined with one another if an abstract factory creates prototypes that can then be cloned without the aid of a factory.

Compound and decorator are often used together with prototypes.

Web links

Individual evidence

  1. Erich Gamma , Richard Helm , Ralph Johnson , John Vlissides : Design pattern . 5th edition. Addison-Wesley , 1996, ISBN 3-8273-1862-9 .