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#
Example of code in JAVA
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 |