The prototype pattern is actually creating another customizable object from one object without needing to know the details of any creation.
. NET provides the ICloneable interface in the System namespace, which is the only method clone (), so that you can only implement this interface to complete the prototype mode. (choose "Big talk design Mode")
The MemberwiseClone () method, if the field is a value type, performs a bitwise copy of the field, and if it is an app type, copies the reference but does not copy the Reference object, so the original object and its replica apply the same object.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceprototype Mode {classc:icloneable {Private stringaddress; Public stringAddress {Get { returnaddress; } Set{Address=value; } } Public ObjectClone () {return(Object) This. MemberwiseClone (); } } classa:icloneable {Private stringname; Private stringsex; Publicc C =NewC (); Public voidSexc (C Sc) { This. C =(C) Sc.clone (); } Public stringName {Get { returnname; } Set{Name=value; } } Public stringSex {Get { returnsex; } Set{Sex=value; } } PublicAstringSname,stringSsex) { This. Name =Sname; This. Sex =Ssex; } Public voidShow () {Console.WriteLine ("name is: {0}", name); Console.WriteLine ("Gender: {0}", Sex); Console.WriteLine ("work location: {0}", c.address); } Public ObjectClone () {return(Object) This. MemberwiseClone (); } } classProgram {Static voidMain (string[] args) {C C=NewC (); C.address="Hubei"; A A=NewA"Zhang Yang","male"); A.sexc (c); C.address="Shenzhen"; A A1=(A) A.clone (); A1. Sexc (c); A.show (); A1.show (); Console.readkey (); } }}
Prototype mode of design pattern