[Guide]
[Design mode sorting Note 1] basic knowledge
[Design mode sorting Note 2] simple factory Mode)
[Design mode sorting Note 3] factory Mode)
[Design pattern sorting Note 4] abstract factory pattern (Abstract Factory)
[Design pattern sorting Note 5] creator pattern (builder)
[Design pattern arrangement Note 6] Summary of factory pattern and creator Pattern
[Design mode sorting Note 7] prototype)
[Design mode sorting Note 8] Singleton)
[Design mode sorting Note 9] appearance mode (facade)
... Later, including some examples
[/Guide]
Simply understand the meaning of the prototype mode, that is, copying an object to another object, but the two objects are independent, but the original object must have a nominal value in the new object, one is the original object and the other is the new object. Note that in the prototype mode, two objects must appear simultaneously. For exampleCodeIn the process, the clone () method is usually used. This is the most typical example of a prototype mode. Here we have understood what the prototype is like! Here is an example:
First, design a class to inherit icloneable, because the clone () method is used:
Code
Using System;
Namespace Consoleapp
{
Public Class Subcompany: icloneable
{
Public Subcompany ( String Name, String ID)
{
This . Name = Name;
This . ID = ID;
This . Headname = " China Mobile " ;
}
// Define group name
Private String Headname;
// Define subsidiary name
Private String Name;
// Define a subsidiary logo ID
Private String ID;
Public StringName
{
Get{ReturnName ;}
Set{Name=Value ;}
}
Public string id
{< br> Get { return ID ;}
set {id = value ;}
}
Public StringHeadname
{
Get{ReturnHeadname ;}
Set{Headname=Value ;}
}
/// <Summary>
/// Return Group Company Name
/// </Summary>
/// <Returns> </returns>
Public String Getheadname ()
{
Return This . Headname;
}
/// <Summary>
/// Return company name
/// </Summary>
/// <Returns> </returns>
Public String Getname ()
{
Return This . Name;
}
/// <Summary>
/// Set company name
/// </Summary>
/// <Param name = "name"> </param>
Public Void Setname ( String Name)
{
This . Name = Name;
}
/// <Summary>
/// Set company ID
/// </Summary>
/// <Param name = "name"> </param>
Public Void Setid ( String ID)
{
This . ID = ID;
}
/// <Summary>
/// Returns the company ID.
/// </Summary>
/// <Returns> </returns>
Public String GETID ()
{
Return This . ID;
}
/// <Summary>
/// Implement the icloneable clone () method
/// </Summary>
/// <Returns> </returns>
Public Object Clone ()
{
Return This . Memberwiseclone ();
}
}
}
Call this class:
Code
Using System;
Namespace Consoleapp
{
Class Program
{
Public Static Void Main ( String [] ARGs)
{
Subcompany subcompanysz = New Subcompany ( " Shenzhen " , " 0755 " ); // Define Shenzhen subsidiary
Console. writeline (subcompanysz. getheadname ());
Console. writeline (subcompanysz. getname ());
Console. writeline (subcompanysz. GETID ());
Subcompany subcompanybj = (Subcompany) subcompanysz. Clone (); // Copy the object to a Beijing subsidiary
Subcompanybj. setname ( " Beijin " );
Subcompanybj. setid ( " 010 " );
// In the following example, the name of a group company does not need to be redefined because Beijing subsidiary is also a group company.
Console. writeline (subcompanysz. getheadname ());
Console. writeline (subcompanybj. getname ());
Console. writeline (subcompanybj. GETID ());
Console. Readline ();
}
}
}
The running result is displayed.