Prototype (owner of design)

See also: Prototype

The prototype Patron of design is used when the creation of a Instance is complex or consuming in time. Rather than to create several authorities of the class, one copies the first authority and one modifies the copy in a suitable way.

To establish this owner a class should be stated abstract specifying a pure virtual method called clone () . All classes requiring a polymorphic manufacturer will derive from this abstract class and will establish the method clone () .

The customer of this class, instead of writing code calling upon the operator " directly; new" on an explicitly known class, the method clone will call () on the prototype or will pass by a mechanism provided by another owner of design (for example a method of manufactures with a parameter indicating the concrete class with instancier).

Structure

The Diagramme of classes is the following:

The class Prototype is used as principal model for the creation of new copies. The classes ConcretePrototype1 and ConcretePrototype2 come to specialize the class Prototype while coming for example to modify certain attributes. The method clone () must turn over a copy of the object concerned. The subclasses can inherit or overload the method clone () . The class Client will undertake to call the methods of cloning via its method operation () .

Example of code in C#

public enum RecordType { Because, Persson } ///

Prototype /// Record is the /// public abstract class Record { public abstract Record Clone (); } /// /// PersonRecord is the Concrete Prototype /// public class PersonRecord: Record { string name; int old; public override Record Clone () { return (Record) this.MemberwiseClone (); // default shallow Copy } } /// /// CarRecord is another Concrete Prototype /// public class CarRecord: Record { string carname; Guid id; public override Record Clone () { CarRecord clone = (CarRecord) this.MemberwiseClone (); // default shallow Copy clone.id = Guid.NewGuid (); // always generate new id return clone; } } /// /// RecordFactory is the customer /// public class RecordFactory { private static Dictionary _prototypes = new Dictionary (); /// /// Constructor /// RecordFactory public () { _prototypes. Add (RecordType.Car, new CarRecord ()); _prototypes. Add (RecordType.Person, new PersonRecord ()); } /// /// The Factory method /// public Record CreateRecord (RecordType standard) { return _prototypes. Clone (); } }

Example of code in JAVA

/* Prototype Class * public class Cookie implements Cloneable { public Object clone () { try { // In year actual implementation off this pattern you would now attach refer to // the expensive to produce shares from the copies that prototype are held inside the. return this.getClass () .newInstance (); } wrestling (InstantiationException E) { e.printStackTrace (); return no one; } } } /* Concrete Prototypes to clone * public class CoconutCookie extends Cookie {} /* Client Class * public class CookieMachine { private Cookie cookie; // could cuts been has private Cloneable cookie; public CookieMachine (Cookie cookie) { this.cookie = cookie; } public Cookie makeCookie () { return (Cookie) cookie.clone (); } public Object clone () {} public static void hand (String args) { Cookie tempCookie = no one; Cookie prot = new CoconutCookie (); CookieMachine cm = new CookieMachine (prot); for (int i=0; i<100; i++) tempCookie = cm.makeCookie (); } }

Examples

Example where prototype applies: let us suppose a class to question a Database. To the instanciation of this class one connects oneself and one recovers the data of the base before carrying out all types of handling. Thereafter it will be more powerful for the future authorities of this class to continue to handle these data than to reinterrogate the base. The first object of connection to the database will have been created directly then initialized. The following objects will be a copy of this one and thus will not require a phase of initialization.

External bonds

Others

  • Explanation of the Prototype in Java with concrete example by Java Village

Random links:Hillbilly | Technetium | Sargon d' Akkad | 7z | Fresnay-the-Samson | Fujiwara No Mototsune | Violoncelle_électrique