1. Why use prototypes? Prototype
For example, we have a toolbar button, a new button, which is an instance of ToolbarButton, which has a length, width attribute, and the like, and attributes are values.
Now we have to add a save button, it is also an instance of ToolbarButton, which also has the length, width attribute, etc., but it has not been assigned, its value is the same as the new button.
If we don't have the prototype design mode, it may be re-assigned. If we use prototype design patterns, we can use the new button as the prototype of the save button. In that case, you don't need to assign a value to the save button. Its default length, the width is the same as the new button.
2. How to use prototypes in C #?
Because the instance of the class is a reference type, you can only use the clone method with the data of the instance in the original class.
Clone method is divided into deep clone and shallow Clone
Provides a light clone method in C #, namely MemberWiseClone ()
C # Shallow Clone example:
Using system;
Namespace DesignPattern
{
Public Class ToolbarButton: iCloneable // Implement Clone Interface
{
Private int _width;
PRIVATE INT_HEIGHT;
Public ToolbarButton ()
{
}
Public int Width
{
get
{
Return_width;
}
set
{
_WIDTH = VALUE;
}
}
Public Int Height
{
get
{
Return_Height;
}
set
{
_Height = value;
}
}
Public Object Clone ()
{
Return this.memberwiseclone (); // Return to Shallow Clone Method ¨
}
}
Public Class Test
{
Public void testmethod ()
{
ToolbarButton MTB_NewButton = New ToolbarButton ();
MTB_NewButton.width = 60;
MTB_NewButton.Height = 30;
ToolbarButton MTB_SAVEBUTTON = New ToolbarButton ();
MTB_SAVEBUTTON = (ToolbarButton) mtb_newbutton.clone ();
// At this time, MTB_SAVEBUTTON has the value of Width and Height.
}
}
}