{"id":109,"date":"2008-03-08T13:32:31","date_gmt":"2008-03-08T13:32:31","guid":{"rendered":""},"modified":"2008-03-08T13:32:31","modified_gmt":"2008-03-08T13:32:31","slug":"109","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=109","title":{"rendered":"\u539f\u578b\u6a21\u5f0f(Prototype Pattern)"},"content":{"rendered":"<p>\u4e00\u3001 \u539f\u578b\uff08Prototype\uff09\u6a21\u5f0f<br \/>\u539f\u578b\u6a21\u5f0f\u7684\u7528\u610f\u662f\uff1a\u901a\u8fc7\u7ed9\u51fa\u4e00\u4e2a\u539f\u578b\u5bf9\u8c61\u6765\u6307\u660e\u6240\u8981\u521b\u5efa\u7684\u5bf9\u8c61\u7c7b\u578b\uff0c\u7136\u540e\u7528\u590d\u5236\u8fd9\u4e2a\u539f\u578b\u5bf9\u8c61\u7684\u529e\u6cd5\u521b\u5efa\u51fa\u66f4\u591a\u7684\u540c\u7c7b\u578b\u5bf9\u8c61\u3002<\/p>\n<p>\u4ece\u5b59\u5927\u5723\u7684\u624b\u6bb5\u8c08\u8d77<\/p>\n<p>\u5b59\u609f\u7a7a\u5728\u4e0e\u9ec4\u98ce\u602a\u7684\u6218\u6597\u4e2d\uff0c&#8221;\u4f7f\u4e00\u4e2a\u8eab\u5916\u8eab\u7684\u624b\u6bb5\uff1a\u628a\u6beb\u6bdb\u63ea\u4e0b\u4e00\u628a\uff0c\u7528\u53e3\u56bc\u5f97\u7c89\u788e\uff0c\u671b\u4e0a\u4e00\u55b7\uff0c\u53eb\u58f0&#8217;\u53d8&#8217;\uff0c\u53d8\u6709\u767e\u5341\u4e2a\u884c\u8005\uff0c\u90fd\u662f\u4e00\u6837\u5f97\u6253\u626e\uff0c\u5404\u6267\u4e00\u6839\u94c1\u68d2\uff0c\u628a\u90a3\u602a\u56f4\u5728\u7a7a\u4e2d\u3002&#8221;\u6362\u800c\u8a00\u4e4b\uff0c\u5b59\u609f\u7a7a\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u5f62\u8c61\uff0c\u590d\u5236\u51fa\u5f88\u591a&#8221;\u8eab\u5916\u8eab&#8221;\u6765\u3002<\/p>\n<p>\u8001\u5b59\u8fd9\u79cd\u8eab\u5916\u8eab\u7684\u624b\u6bb5\u5728\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u9886\u57df\u91cc\u53eb\u539f\u578b\uff08Prototype\uff09\u6a21\u5f0f\u3002<\/p>\n<p>C#\u5bf9\u539f\u578b\u6a21\u5f0f\u7684\u652f\u6301<\/p>\n<p>\u5728C#\u91cc\u9762\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u901a\u8fc7Clone()\u65b9\u6cd5\u5b9e\u73b0\u539f\u578b\u6a21\u5f0f\u3002\u4efb\u4f55\u7c7b\uff0c\u53ea\u8981\u60f3\u652f\u6301\u514b\u9686\uff0c\u5fc5\u987b\u5b9e\u73b0C#\u4e2d\u7684ICloneable\u63a5\u53e3\u3002ICloneable\u63a5\u53e3\u4e2d\u6709\u4e00Clone\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u7c7b\u4e2d\u590d\u5199\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u514b\u9686\u65b9\u6cd5\u3002\u514b\u9686\u7684\u5b9e\u73b0\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u6d45\u62f7\u8d1d\uff08shallow copy\uff09\u4e0e\u6df1\u62f7\u8d1d\uff08deep copy\uff09\u3002<\/p>\n<p>\uff08\u4ee5\u4e0b\u6458\u81ea\uff1a\u300a.NET\u6846\u67b6\u7a0b\u5e8f\u8bbe\u8ba1\uff08\u4fee\u8ba2\u7248\uff09\u300b\uff0c\u674e\u5efa\u5fe0\u8bd1\uff09\u6d45\u62f7\u8d1d\u662f\u6307\u5f53\u5bf9\u8c61\u7684\u5b57\u6bb5\u503c\u88ab\u62f7\u8d1d\u65f6\uff0c\u5b57\u6bb5\u5f15\u7528\u7684\u5bf9\u8c61\u4e0d\u4f1a\u88ab\u62f7\u8d1d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u6709\u4e00\u4e2a\u6307\u5411\u5b57\u7b26\u4e32\u7684\u5b57\u6bb5\uff0c\u5e76\u4e14\u6211\u4eec\u5bf9\u8be5\u5bf9\u8c61\u505a\u4e86\u4e00\u4e2a\u6d45\u62f7\u8d1d\uff0c\u90a3\u4e48\u4e24\u4e2a\u5bf9\u8c61\u5c06\u5f15\u7528\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u800c\u6df1\u62f7\u8d1d\u662f\u5bf9\u5bf9\u8c61\u5b9e\u4f8b\u4e2d\u5b57\u6bb5\u5f15\u7528\u7684\u5bf9\u8c61\u4e5f\u8fdb\u884c\u62f7\u8d1d\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u6240\u4ee5\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u6709\u4e00\u4e2a\u6307\u5411\u5b57\u7b26\u4e32\u7684\u5b57\u6bb5\uff0c\u5e76\u4e14\u6211\u4eec\u5bf9\u8be5\u5bf9\u8c61\u505a\u4e86\u4e00\u4e2a\u6df1\u62f7\u8d1d\u7684\u8bdd\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u548c\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32&#8211;\u65b0\u5bf9\u8c61\u5c06\u5f15\u7528\u65b0\u5b57\u7b26\u4e32\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u6267\u884c\u6df1\u62f7\u8d1d\u540e\uff0c\u539f\u6765\u7684\u5bf9\u8c61\u548c\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u4e0d\u4f1a\u5171\u4eab\u4efb\u4f55\u4e1c\u897f\uff1b\u6539\u53d8\u4e00\u4e2a\u5bf9\u8c61\u5bf9\u53e6\u5916\u4e00\u4e2a\u5bf9\u8c61\u6ca1\u6709\u4efb\u4f55\u5f71\u54cd\u3002<\/p>\n<p>\u4e8c\u3001 Prototype\u6a21\u5f0f\u7684\u7ed3\u6784\uff1a<br \/><img decoding=\"async\" src=\"http:\/\/www.poloo.org\/wp-content\/attachments\/month_0803\/u200838133030.gif\" \/><br \/>\u5ba2\u6237\uff08Client\uff09\u89d2\u8272\uff1a\u5ba2\u6237\u7c7b\u63d0\u51fa\u521b\u5efa\u5bf9\u8c61\u7684\u8bf7\u6c42\u3002<br \/>\u62bd\u8c61\u539f\u578b\uff08Prototype\uff09\u89d2\u8272\uff1a\u8fd9\u662f\u4e00\u4e2a\u62bd\u8c61\u89d2\u8272\uff0c\u901a\u5e38\u7531\u4e00\u4e2aC#\u63a5\u53e3\u6216\u62bd\u8c61\u7c7b\u5b9e\u73b0\u3002\u6b64\u89d2\u8272\u7ed9\u51fa\u6240\u6709\u7684\u5177\u4f53\u539f\u578b\u7c7b\u6240\u9700\u7684\u63a5\u53e3\u3002\u5728C#\u4e2d\uff0c\u62bd\u8c61\u539f\u578b\u89d2\u8272\u901a\u5e38\u5b9e\u73b0\u4e86ICloneable\u63a5\u53e3\u3002<br \/>\u5177\u4f53\u539f\u578b\uff08Concrete Prototype\uff09\u89d2\u8272\uff1a\u88ab\u590d\u5236\u7684\u5bf9\u8c61\u3002\u6b64\u89d2\u8272\u9700\u8981\u5b9e\u73b0\u62bd\u8c61\u539f\u578b\u89d2\u8272\u6240\u8981\u6c42\u7684\u63a5\u53e3\u3002<\/p>\n<p>\u4e09\u3001 \u7a0b\u5e8f\u4e3e\u4f8b\uff1a<br \/>\u4e0b\u9762\u7684\u7a0b\u5e8f\u7ed9\u51fa\u4e86\u4e00\u4e2a\u793a\u610f\u6027\u7684\u5b9e\u73b0\uff1a<br \/><code>\/\/ Prototype pattern -- Structural example&nbsp;&nbsp;<br \/>using System;<\/p>\n<p>\/\/ \"Prototype\"<br \/>abstract class Prototype<br \/>{<br \/>&nbsp;&nbsp;\/\/ Fields<br \/>&nbsp;&nbsp;private string id;<\/p>\n<p>&nbsp;&nbsp;\/\/ Constructors<br \/>&nbsp;&nbsp;public Prototype( string id )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;this.id = id;<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;public string Id<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;get{ return id; }<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;abstract public Prototype Clone();<br \/>}<\/p>\n<p>\/\/ \"ConcretePrototype1\"<br \/>class ConcretePrototype1 : Prototype<br \/>{<br \/>&nbsp;&nbsp;\/\/ Constructors<br \/>&nbsp;&nbsp;public ConcretePrototype1( string id ) : base ( id ) {}<\/p>\n<p>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;override public Prototype Clone()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Shallow copy<br \/>&nbsp;&nbsp;&nbsp;&nbsp;return (Prototype)this.MemberwiseClone();<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ \"ConcretePrototype2\"<br \/>class ConcretePrototype2 : Prototype<br \/>{<br \/>&nbsp;&nbsp;\/\/ Constructors<br \/>&nbsp;&nbsp;public ConcretePrototype2( string id ) : base ( id ) {}<\/p>\n<p>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;override public Prototype Clone()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Shallow copy<br \/>&nbsp;&nbsp;&nbsp;&nbsp;return (Prototype)this.MemberwiseClone();<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/**\/\/\/\/ &lt;summary&gt;<br \/>\/\/\/ Client test<br \/>\/\/\/ &lt;\/summary&gt;<br \/>class Client<br \/>{<br \/>&nbsp;&nbsp;public static void Main( string[] args )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Create two instances and clone each<br \/>&nbsp;&nbsp;&nbsp;&nbsp;ConcretePrototype1 p1 = new ConcretePrototype1( \"I\" );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( \"Cloned: {0}\", c1.Id );<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;ConcretePrototype2 p2 = new ConcretePrototype2( \"II\" );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( \"Cloned: {0}\", c2.Id );<br \/>&nbsp;&nbsp;}<br \/>}<br \/><\/code><\/p>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u5b9e\u73b0\u4e86\u4e00\u4e2a\u6d45\u62f7\u8d1d\u3002\u5176\u4e2dMemberwiseClone()\u65b9\u6cd5\u662fObject\u7c7b\u7684\u4e00\u4e2a\u53d7\u4fdd\u62a4\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4e86\u5bf9\u8c61\u7684\u6d45\u62f7\u8d1d\u3002\u5982\u679c\u5e0c\u671b\u5b9e\u73b0\u4e00\u4e2a\u6df1\u62f7\u8d1d\uff0c\u5e94\u8be5\u5b9e\u73b0ICloneable\u63a5\u53e3\uff0c\u5e76\u81ea\u5df1\u7f16\u5199ICloneable\u7684Clone\u63a5\u53e3\u65b9\u6cd5\u3002<\/p>\n<p>\u56db\u3001 \u5e26Prototype Manager\u7684\u539f\u578b\u6a21\u5f0f<br \/>\u539f\u578b\u6a21\u5f0f\u7684\u7b2c\u4e8c\u79cd\u5f62\u5f0f\u662f\u5e26\u539f\u578b\u7ba1\u7406\u5668\u7684\u539f\u578b\u6a21\u5f0f\uff0c\u5176UML\u56fe\u5982\u4e0b\uff1a<br \/><img decoding=\"async\" src=\"http:\/\/www.poloo.org\/wp-content\/attachments\/month_0803\/9200838133044.gif\" \/><br \/>\u5ba2\u6237\uff08Client\uff09\u89d2\u8272\uff1a\u5ba2\u6237\u7aef\u7c7b\u5411\u539f\u578b\u7ba1\u7406\u5668\u63d0\u51fa\u521b\u5efa\u5bf9\u8c61\u7684\u8bf7\u6c42\u3002<br \/>\u62bd\u8c61\u539f\u578b\uff08Prototype\uff09\u89d2\u8272\uff1a\u8fd9\u662f\u4e00\u4e2a\u62bd\u8c61\u89d2\u8272\uff0c\u901a\u5e38\u7531\u4e00\u4e2aC#\u63a5\u53e3\u6216\u62bd\u8c61\u7c7b\u5b9e\u73b0\u3002\u6b64\u89d2\u8272\u7ed9\u51fa\u6240\u6709\u7684\u5177\u4f53\u539f\u578b\u7c7b\u6240\u9700\u7684\u63a5\u53e3\u3002\u5728C#\u4e2d\uff0c\u62bd\u8c61\u539f\u578b\u89d2\u8272\u901a\u5e38\u5b9e\u73b0\u4e86ICloneable\u63a5\u53e3\u3002<br \/>\u5177\u4f53\u539f\u578b\uff08Concrete Prototype\uff09\u89d2\u8272\uff1a\u88ab\u590d\u5236\u7684\u5bf9\u8c61\u3002\u6b64\u89d2\u8272\u9700\u8981\u5b9e\u73b0\u62bd\u8c61\u7684\u539f\u578b\u89d2\u8272\u6240\u8981\u6c42\u7684\u63a5\u53e3\u3002<br \/>\u539f\u578b\u7ba1\u7406\u5668\uff08Prototype Manager\uff09\u89d2\u8272\uff1a\u521b\u5efa\u5177\u4f53\u539f\u578b\u7c7b\u7684\u5bf9\u8c61\uff0c\u5e76\u8bb0\u5f55\u6bcf\u4e00\u4e2a\u88ab\u521b\u5efa\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u6f14\u793a\u4e86\u5728\u539f\u578b\u7ba1\u7406\u5668\u4e2d\u5b58\u50a8\u7528\u6237\u9884\u5148\u5b9a\u4e49\u7684\u989c\u8272\u539f\u578b\uff0c\u5ba2\u6237\u901a\u8fc7\u539f\u578b\u7ba1\u7406\u5668\u514b\u9686\u989c\u8272\u5bf9\u8c61\u3002<\/p>\n<p><code>\/\/ Prototype pattern -- Real World example&nbsp;&nbsp;<br \/>using System;<br \/>using System.Collections;<\/p>\n<p>\/\/ \"Prototype\"<br \/>abstract class ColorPrototype<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;public abstract ColorPrototype Clone();<br \/>}<\/p>\n<p>\/\/ \"ConcretePrototype\"<br \/>class Color : ColorPrototype<br \/>{<br \/>&nbsp;&nbsp;\/\/ Fields<br \/>&nbsp;&nbsp;private int red, green, blue;<\/p>\n<p>&nbsp;&nbsp;\/\/ Constructors<br \/>&nbsp;&nbsp;public Color( int red, int green, int blue)<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;this.red = red;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;this.green = green;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;this.blue = blue;<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;public override ColorPrototype Clone()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Creates a 'shallow copy'<br \/>&nbsp;&nbsp;&nbsp;&nbsp;return (ColorPrototype) this.MemberwiseClone();<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;public void Display()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( \"RGB values are: {0},{1},{2}\",<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;red, green, blue );<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ Prototype manager<br \/>class ColorManager<br \/>{<br \/>&nbsp;&nbsp;\/\/ Fields<br \/>&nbsp;&nbsp;Hashtable colors = new Hashtable();<\/p>\n<p>&nbsp;&nbsp;\/\/ Indexers<br \/>&nbsp;&nbsp;public ColorPrototype this[ string name ]<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;get{ return (ColorPrototype)colors[ name ]; }<br \/>&nbsp;&nbsp;&nbsp;&nbsp;set{ colors.Add( name, value ); }<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/**\/\/\/\/ &lt;summary&gt;<br \/>\/\/\/&nbsp;&nbsp;PrototypeApp test<br \/>\/\/\/ &lt;\/summary&gt;<br \/>class PrototypeApp<br \/>{<br \/>&nbsp;&nbsp;public static void Main( string[] args )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;ColorManager colormanager = new ColorManager();<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Initialize with standard colors<br \/>&nbsp;&nbsp;&nbsp;&nbsp;colormanager[ \"red\" ] = new Color( 255, 0, 0 );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;colormanager[ \"green\" ] = new Color( 0, 255, 0 );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;colormanager[ \"blue\" ] = new Color( 0, 0, 255 );<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ User adds personalized colors<br \/>&nbsp;&nbsp;&nbsp;&nbsp;colormanager[ \"angry\" ] = new Color( 255, 54, 0 );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;colormanager[ \"peace\" ] = new Color( 128, 211, 128 );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;colormanager[ \"flame\" ] = new Color( 211, 34, 20 );<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ User uses selected colors<br \/>&nbsp;&nbsp;&nbsp;&nbsp;string colorName = \"red\";<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Color c1 = (Color)colormanager[ colorName ].Clone();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;c1.Display();<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;colorName = \"peace\";<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Color c2 = (Color)colormanager[ colorName ].Clone();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;c2.Display();<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;colorName = \"flame\";<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Color c3 = (Color)colormanager[ colorName ].Clone();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;c3.Display();<br \/>&nbsp;&nbsp;}<br \/>}<br \/><\/code><\/p>\n<p>\u4e94\u3001 \u6d45\u62f7\u8d1d\u4e0e\u6df1\u62f7\u8d1d<br \/>\u4e0b\u9762\u7ed9\u51fa\u6d45\u62f7\u8d1d\u4e0e\u6df1\u62f7\u8d1d\u7684\u4e24\u4e2a\u4f8b\u5b50\uff0c\u4f8b\u5b50\u4f7f\u7528\u4e86ICloneable\u63a5\u53e3\u3002C#\u4e2d\u7684\u6570\u7ec4\u662f\u5f15\u7528\u578b\u7684\u53d8\u91cf\uff0c\u6211\u4eec\u901a\u8fc7\u6570\u7ec4\u6765\u8fdb\u884c\u6f14\u793a\uff1a<\/p>\n<p>\u6d45\u62f7\u8d1d\uff1a<br \/><code>using System;<\/p>\n<p>class ShallowCopy : ICloneable<br \/>{<br \/>&nbsp;&nbsp;public int[] v = {1,2,3};<\/p>\n<p>&nbsp;&nbsp;public Object Clone()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;return this.MemberwiseClone();<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;public void Display()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;foreach(int i in v)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.Write( i + \", \");<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine();<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>class Client<br \/>{<br \/>&nbsp;&nbsp;public static void Main()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;ShallowCopy sc1 = new ShallowCopy();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;ShallowCopy sc2 = (ShallowCopy)sc1.Clone();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;sc1.v[0] = 9;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;sc1.Display();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;sc2.Display();<br \/>&nbsp;&nbsp;}<br \/>}<br \/><\/code><\/p>\n<p>ShallowCopy\u5bf9\u8c61\u5b9e\u73b0\u4e86\u4e00\u4e2a\u6d45\u62f7\u8d1d\uff0c\u56e0\u6b64\u5f53\u5bf9sc1\u8fdb\u884c\u514b\u9686\u65f6\uff0c\u5176\u5b57\u6bb5v\u5e76\u6ca1\u6709\u514b\u9686\uff0c\u8fd9\u5bfc\u81f4sc1\u4e0esc2\u7684\u5b57\u6bb5v\u90fd\u6307\u5411\u4e86\u540c\u4e00\u4e2av\uff0c\u56e0\u6b64\uff0c\u5f53\u4fee\u6539\u4e86sc1\u7684v[0]\u540e\uff0csc2\u7684v[0]\u4e5f\u53d1\u751f\u4e86\u53d8\u5316\u3002<\/p>\n<p>\u6df1\u62f7\u8d1d\uff1a<br \/><code>using System;<\/p>\n<p>class DeepCopy : ICloneable<br \/>{<br \/>&nbsp;&nbsp;public int[] v = {1,2,3};<\/p>\n<p>&nbsp;&nbsp;\/\/ \u9ed8\u8ba4\u6784\u9020\u51fd\u6570<br \/>&nbsp;&nbsp;public DeepCopy()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;\/\/ \u4f9bClone\u65b9\u6cd5\u8c03\u7528\u7684\u79c1\u6709\u6784\u9020\u51fd\u6570<br \/>&nbsp;&nbsp;private DeepCopy(int[] v)<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;this.v = (int[])v.Clone();<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;public Object Clone()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u6784\u9020\u4e00\u4e2a\u65b0\u7684DeepCopy\u5bf9\u8c61\uff0c\u6784\u9020\u53c2\u6570\u4e3a<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u539f\u6709\u5bf9\u8c61\u4e2d\u4f7f\u7528\u7684 v <br \/>&nbsp;&nbsp;&nbsp;&nbsp;return new DeepCopy(this.v);<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;public void Display()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;foreach(int i in v)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.Write( i + \", \");<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine();<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>class Client<br \/>{<br \/>&nbsp;&nbsp;public static void Main()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;DeepCopy dc1 = new DeepCopy();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;DeepCopy dc2 = (DeepCopy)dc1.Clone();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;dc1.v[0] = 9;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;dc1.Display();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;dc2.Display();<br \/>&nbsp;&nbsp;}<br \/>}<br \/><\/code><\/p>\n<p>\u8fd9\u6b21\u5728\u514b\u9686\u7684\u65f6\u5019\uff0c\u4e0d\u4f46\u514b\u9686\u5bf9\u8c61\u672c\u8eab\uff0c\u8fde\u91cc\u9762\u7684\u6570\u7ec4\u5b57\u6bb5\u4e00\u5e76\u514b\u9686\u3002\u56e0\u6b64\uff0c\u6700\u7ec8\u6253\u5370\u51fa\u6765\u7684dc1\u4e0edc2\u4e0d\u540c\u3002<\/p>\n<p>\u516d\u3001 Prototype\u6a21\u5f0f\u7684\u4f18\u70b9\u4e0e\u7f3a\u70b9<br \/>Prototype\u6a21\u5f0f\u7684\u4f18\u70b9\u5305\u62ec<\/p>\n<p>1\u3001Prototype\u6a21\u5f0f\u5141\u8bb8\u52a8\u6001\u589e\u52a0\u6216\u51cf\u5c11\u4ea7\u54c1\u7c7b\u3002\u7531\u4e8e\u521b\u5efa\u4ea7\u54c1\u7c7b\u5b9e\u4f8b\u7684\u65b9\u6cd5\u662f\u4ea7\u6279\u7c7b\u5185\u90e8\u5177\u6709\u7684\uff0c\u56e0\u6b64\u589e\u52a0\u65b0\u4ea7\u54c1\u5bf9\u6574\u4e2a\u7ed3\u6784\u6ca1\u6709\u5f71\u54cd\u3002<\/p>\n<p>2\u3001Prototype\u6a21\u5f0f\u63d0\u4f9b\u4e86\u7b80\u5316\u7684\u521b\u5efa\u7ed3\u6784\u3002\u5de5\u5382\u65b9\u6cd5\u6a21\u5f0f\u5e38\u5e38\u9700\u8981\u6709\u4e00\u4e2a\u4e0e\u4ea7\u54c1\u7c7b\u7b49\u7ea7\u7ed3\u6784\u76f8\u540c\u7684\u7b49\u7ea7\u7ed3\u6784\uff0c\u800cPrototype\u6a21\u5f0f\u5c31\u4e0d\u9700\u8981\u8fd9\u6837\u3002<\/p>\n<p>3\u3001Portotype\u6a21\u5f0f\u5177\u6709\u7ed9\u4e00\u4e2a\u5e94\u7528\u8f6f\u4ef6\u52a8\u6001\u52a0\u8f7d\u65b0\u529f\u80fd\u7684\u80fd\u529b\u3002\u7531\u4e8ePrototype\u7684\u72ec\u7acb\u6027\u8f83\u9ad8\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u52a8\u6001\u52a0\u8f7d\u65b0\u529f\u80fd\u800c\u4e0d\u5f71\u54cd\u8001\u7cfb\u7edf\u3002<\/p>\n<p>4\u3001\u4ea7\u54c1\u7c7b\u4e0d\u9700\u8981\u975e\u5f97\u6709\u4efb\u4f55\u4e8b\u5148\u786e\u5b9a\u7684\u7b49\u7ea7\u7ed3\u6784\uff0c\u56e0\u4e3aPrototype\u6a21\u5f0f\u9002\u7528\u4e8e\u4efb\u4f55\u7684\u7b49\u7ea7\u7ed3\u6784\u3002<\/p>\n<p>Prototype\u6a21\u5f0f\u7684\u7f3a\u70b9\uff1a<\/p>\n<p>Prototype\u6a21\u5f0f\u7684\u6700\u4e3b\u8981\u7f3a\u70b9\u5c31\u662f\u6bcf\u4e00\u4e2a\u7c7b\u5fc5\u987b\u914d\u5907\u4e00\u4e2a\u514b\u9686\u65b9\u6cd5\u3002\u800c\u4e14\u8fd9\u4e2a\u514b\u9686\u65b9\u6cd5\u9700\u8981\u5bf9\u7c7b\u7684\u529f\u80fd\u8fdb\u884c\u901a\u76d8\u8003\u8651\uff0c\u8fd9\u5bf9\u5168\u65b0\u7684\u7c7b\u6765\u8bf4\u4e0d\u662f\u5f88\u96be\uff0c\u4f46\u5bf9\u5df2\u6709\u7684\u7c7b\u8fdb\u884c\u6539\u9020\u65f6\uff0c\u4e0d\u4e00\u5b9a\u662f\u4ef6\u5bb9\u6613\u7684\u4e8b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001 \u539f\u578b\uff08Prototype\uff09\u6a21\u5f0f\u539f\u578b\u6a21\u5f0f\u7684\u7528\u610f\u662f\uff1a\u901a\u8fc7\u7ed9\u51fa\u4e00\u4e2a\u539f&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-Note"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=109"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}