Prototype mode of design pattern

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.