Prototype mode (Prototype pattern)
Introduced
Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototype.
Example
There is a message entity class, and now you want to clone it.
Messagemodel
Using system;using system.collections.generic;using system.text;namespace pattern.prototype{//<summary>// Message entity class///</summary> public class Messagemodel {//<summary>///constructor// /</summary>//<param name= "msg" >message content </param>//<param name= "PT" >message when released Room </param> public Messagemodel (String msg, DateTime pt) {this._message = msg; This._publishtime = pt; } private string _message; <summary>//message content///</summary> public string Message {get {return _message;} set {_message = value;} } private DateTime _publishtime; <summary>//Message release time///</summary> public DateTime Publishtime { get {return _publishtime;} set {_publishtime = value;} }}}
Shallowcopy
Using system;using system.collections.generic;using system.text;namespace pattern.prototype{// <summary >//Light copy/// </summary> public class shallowcopy:icloneable { //<summary> ///constructor///</summary> public shallowcopy () {}///<summary>// Implement the Clone () method for icloneable///</summary>/// <returns></returns> public Object Clone () { return this. MemberwiseClone (); } Private Messagemodel _mm; <summary>// Message Entity object/// </summary> public Messagemodel Messagemodel { get {return _mm;} set {_mm = value;}}}}
Deepcopy
Using system;using system.collections.generic;using system.text;namespace pattern.prototype{//<summary>// Deep copy///</summary> public class Deepcopy:icloneable {//<summary>///constructors </summary> public deepcopy () {}///<summary>//constructor </summary>//<param name= "MM" >message solid objects </param> public deepcopy (Messagemodel m m) {_mm = mm; }///<summary>//Implement ICloneable's Clone () method///</summary>//<returns></ Returns> public Object Clone () {return new deepcopy (New Messagemodel (_mm. Message, _mm. Publishtime)); } private Messagemodel _mm; <summary>//Message Entity object///</summary> public Messagemodel Messagemodel { get {return _mm;} set {_mm = value; } } }}
Client
Using system;using system.data;using system.configuration;using system.collections;using System.Web;using System.web.security;using system.web.ui;using system.web.ui.webcontrols;using System.Web.UI.WebControls.WebParts; Using system.web.ui.htmlcontrols;using pattern.prototype;public partial class prototype:system.web.ui.page{Protecte d void Page_Load (object sender, EventArgs e) {Response.Write ("Shallowcopy demo as follows: <br/>"); Showshallowcopy (); Response.Write ("Deepcopy demo as follows: <br/>"); Showdeepcopy (); } private void Showshallowcopy () {shallowcopy sc = new shallowcopy (); Sc. Messagemodel = new Messagemodel ("Shallowcopy", DateTime.Now); Shallowcopy SC2 = (shallowcopy) sc. Clone (); Response.Write (SC. Messagemodel.message); Response.Write ("<br/>"); Response.Write (SC2. Messagemodel.message); Response.Write ("<br/>"); Sc. Messagemodel.message = "Shallowcopyshallowcopy";Response.Write (SC. Messagemodel.message); Response.Write ("<br/>"); Response.Write (SC2. Messagemodel.message); Response.Write ("<br/>"); } private void Showdeepcopy () {deepcopy sc = new deepcopy (); Sc. Messagemodel = new Messagemodel ("Deepcopy", DateTime.Now); Deepcopy SC2 = (deepcopy) sc. Clone (); Response.Write (SC. Messagemodel.message); Response.Write ("<br/>"); Response.Write (SC2. Messagemodel.message); Response.Write ("<br/>"); Sc. Messagemodel.message = "Deepcopydeepcopy"; Response.Write (SC. Messagemodel.message); Response.Write ("<br/>"); Response.Write (SC2. Messagemodel.message); Response.Write ("<br/>"); }}
Run results
The Shallowcopy demo is as follows:
Shallowcopy
Shallowcopy
Shallowcopyshallowcopy
Shallowcopyshallowcopy
The Deepcopy demo is as follows:
Deepcopy
Deepcopy
Deepcopydeepcopy
Deepcopy
24 Design modes: Prototype mode (Prototype pattern)