Design mode-prototype mode

Source: Internet
Author: User

The prototype pattern is literally, probably not so popular, and in layman's terms, it can be said to be copy mode.

From the copy, there is a complete copy, and not a complete copy. As if the Sun Monkey's blowing musang monkeys, but these little monkeys obviously no monkey king, this copy, is a shallow copy of it.

Since there is a shallow copy, it must also have a deep copy. Deep copy is the small monkey and Monkey King body as strong, whether from the ability or the appearance, are the same

First, the prototype

/// <summary>///prototypes/// </summary>Abstract  Public classprototype{ Public intId {Get;Set; } Public stringName {Get;Set; } Publiclist<int> Size =Newlist<int>(); PublicPrototype (intID) {     This. Id =ID;}Abstract  PublicPrototype Clone ();}

On the prototype, I put a value type, a string, an shaping set, in prototype mode, I will modify these values to observe the changes after the clone

Second, shallow copy

 Public class concrete1:prototype{    public Concrete1 (int  ID)         base(ID)    { }      Public Override Prototype Clone ()    {        return (Prototype)this. MemberwiseClone ();    }}

Test code:

Console.WriteLine ("--------------Shallow Copy----------------------"); Concrete1 P1=NewConcrete1 (1);p 1. Name="Name";p 1. Size.addrange (New int[] {1,2,3,4,5 }); Concrete1 C1=(Concrete1) p1. Clone (); Console.WriteLine ("before Concrete1 change"); Console.WriteLine ("Cloned:id-{0}, Name-{1}, Size-{2}", C1. Id, C1. Name,string. Join (",", C1. Size)); Console.WriteLine ();p 1. Id=2;p 1. Name="P1. Name";p 1. Size.addrange (New int[] {6,7,8,9 }); Console.WriteLine ("After Concrete1 changed"); Console.WriteLine ("Cloned:id-{0}, Name-{1}, Size-{2}", C1. Id, C1. Name,string. Join (",", C1. Size));

Results:

As you can see, after cloning, I modified the values of the prototype collection, and the cloned objects changed, indicating that their set variables point to the same heap space.

Console.WriteLine ("p1. Size = = C1. Size, {0}", p1. Size = = C1. Size);

There is a picture of the truth.

Third, deep copy

 Public class concrete2:prototype{    public Concrete2 (int  ID)         base(ID)    { }      Public Override Prototype Clone ()    {var jsonstr = Jsonconvert.serializeobject (this); return jsonconvert.deserializeobject<concrete2> (JSONSTR);    }}

Test code:

Console.WriteLine ("--------------deep Copy----------------------"); Concrete2 P2=NewConcrete2 (2);p 2. Name="Name";p 2. Size.addrange (New int[] {1,2,3,4,5 }); Concrete2 C2=(Concrete2) P2. Clone (); Console.WriteLine ("before Concrete2 change"); Console.WriteLine ("Cloned:id-{0}, Name-{2}, Size-{2}", C2. Id, C2. Name,string. Join (",", C2. Size)); Console.WriteLine ();p 2. Id=2;p 2. Name="P2. Name";p 2. Size.addrange (New int[] {6,7,8,9 }); Console.WriteLine ("After Concrete2 changed"); Console.WriteLine ("Cloned:id-{0}, Name-{2}, Size-{2}", C2. Id, C2. Name,string. Join (",", C2. Size)); Console.WriteLine ("P2. Size = = C2. Size, {0}", p2. Size = = C2. Size);

Results:

As you can see, the Copy entities do not change, stating that they are already two separate entities. There are no common variable references (except for method references).

Here, the way I implement deep copies is through serialization, and of course there are many other ways. Can be implemented on its own. The Clone method provided by C # is the implementation of a shallow copy.

C # has an interface: ICloneable, if you do not want to make an abstract class, can also be implemented through this interface.

Iv. Personal Application

For my own application in the project, I have used structural cloning.

Datatble this variable, whether B/s, or C/s, should have been used it.

It has a clone method that can be used to clone table structures and constraints, which is quite handy, and I don't need to do the complicated table structure creation again.

Reference:

Liar design Mode

C # design mode (9)

Design mode-prototype mode

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.