The prototype mode specifies the types of objects to be created, and creates new objects by copying these prototypes. The prototype mode allows an object to create another custom object without any details. Working principle: by passing a prototype object to the object to be created, the object to be created will be executed by requesting the prototype object to copy the prototype itself.
The UML class diagram is as follows:
The relationships between classes and objects are as follows:
1. Prototype (Abstract prototype class): defines interfaces with methods to clone itself.
2. concreteprototype (Specific prototype): implements a specific cloning method.
3. clinet (customer): generate a new object by cloning.
The following figure shows the order of typical applications:
- Instance 1 -- color Manager
The color manager first creates various colors and saves them in a hash table. When needed, a clone of the color is returned.
Its Class Structure
Code
// Define an abstract prototype
Abstract Class Colorprototype
{
// Clone Methods clone method
Public Abstract Colorprototype clone ();
}
// Concreteprototype
Class Color: colorprototype
{
Private Int Red;
Private Int Green;
Private Int Blue;
Public Color ( Int Red, Int Green, Int Blue)
{
This . Red = Red;
This . Green = Green;
This . Blue = Blue;
}
Public Override Colorprototype clone ()
{
// Create a superficial copy
Return (Colorprototype) This . Memberwiseclone ();
}
Public Void Display ()
{
Console. writeline ( " RGB values are: {0}, {1}, {2} " , Red, green, blue );
}
}
// Prototype Manager
Class Colormanager
{
Hashtable colors = New Hashtable ();
Public Colorprototype This [ String Name]
{
Get { Return (Colorprototype) colors [name];}
Set {Colors. Add (name, value );}
}
}
// Customer application testing
Class Client
{
[Stathread]
Static Void Main ( String [] ARGs)
{
Colormanager = New Colormanager ();
// Initialize Standard colors
Colormanager [ " Red " ] = New Color ( 255 , 0 , 0 );
Colormanager [ " Green " ] = New Color ( 0 , 255 , 0 );
Colormanager [ " Blue " ] = New Color ( 0 , 0 , 255 );
// Add personalized colors
Colormanager [ " Angry " ] = New Color ( 255 , 54 , 0 );
Colormanager [ " Peace " ] = New Color ( 128 , 211 , 128 );
Colormanager [ " Flame " ] = New Color ( 211 , 34 , 20 );
// The specified color is selected.
String Colorname = " Red " ;
Color C1 = (Color) colormanager [colorname]. Clone ();
C1.display ();
Colorname = " Peace " ;
Color C2 = (Color) colormanager [colorname]. Clone ();
C2.display ();
Colorname = " Flame " ;
Color C3 = (Color) colormanager [colorname]. Clone ();
C3.display ();
Console. Read ();
}
}
The prototype mode has been widely used, especially when the object creation cost is large (initialization takes a long time, occupying too many CPU resources or network resources. For example, to create an object through WebService or DCOM, or to create an object to load a large file, if the system needs to reuse it, the new object can be obtained by copying and slightly modifying the attributes of an existing object in the prototype mode. In addition, if the system wants to save the object state and the object state changes little, or the object itself occupies a small amount of memory, it can also be applied in the prototype mode with the memorandum mode. On the contrary, if the object state changes significantly or the object occupies a large amount of memory, the state mode is better than the prototype mode. The disadvantage of the prototype mode is that you need to write complicatedCode.
The following scenario is suitable for the application prototype:
1. Class instantiation is dynamic.
2. Avoid using hierarchical factory classes to create hierarchical objects.
3. the Instance Object of the class has only one or a few combined states.