. NET prototype model explained

Source: Internet
Author: User
Definition of prototype pattern:

Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes.

Prototype pattern structure diagram:

A special mode in the creation mode-prototype mode, one of the most important feature is to clone an existing object, the result of this clone has 2 kinds, one is the shallow copy, the other is the deep copy.

The creation pattern is typically used to create a new object, and then we use this object to do some object manipulation, and we can quickly create an object without having to provide a special new () operation, which is a very efficient way to do it. Quickly create a new object.

1. Prototype mode: Shallow copy

Defines an interface that is used to describe all the color object interfaces

<summary>///Color Interface  ///</summary>public interface icolor{icolor Clone ();  int Red {get; set;}  int Green {get; set;}  int Blue {get; set;}}

Give the concrete implementation code of red:

public class redcolor:icolor{Public  int Red {get; set;}  public int Green {get; set;}  public int Blue {get; set;}   Public Icolor Clone ()  {    return (icolor) this. MemberwiseClone ();  }}

The specific test code is as follows:

static void Main (string[] args) {  icolor color = new Redcolor ();  Color. Red = 255;  Console.WriteLine ("color-red" + color.) Red); 225  icolor Color1 = color. Clone ();  Color1. Red = 224;  Console.WriteLine ("Color1-red" + color1. Red);//224  Console.WriteLine ("color-red" + color. Red); 225}

It can be found that when we modify the Red property value of the Color1 object, there is no effect on the color's attribute, that is, the modification of the object's copy does not affect the state of the object itself.

2. Prototype mode: Deep copy

Deep replication considerations are relatively complex, because it is possible for objects to have an inheritance or reference relationship, and we may need to be careful when we copy them deeply. In general, deep copy can be used to make a simple deep copy of the object, and it can be copied in the form of a serialized object. The prototype pattern is implemented as a serialized form:

Using system;using system.collections.generic;using system.linq;using system.text;namespace ConsoleApplication4{//     <summary>///color interface///</summary> public interface Icolor {Icolordemo Clone ();    int Red {get; set;}    int Green {get; set;}    int Blue {get; set;}  Factroy F{get;set;} }///<summary>//Production of color factory information///</summary> [Serializable] public class Factroy {public string Nam  e {get; set;} }} using system;using system.collections.generic;using system.linq;using system.text;namespace ConsoleApplication4{/  <summary>///color///</summary> [Serializable] public class Redcolor:icolor {public int Red {get; Set    } public int Green {get; set;}    public int Blue {get; set;}     Public Factroy f {get; set;}      Public Icolor Clone () {serializablehelper s = new Serializablehelper ();      String target = S.serializable (this);    return s.derializable<icolor> (target); }  }}

Serialization Helper Classes:

<summary>  ///Serialization and deserialization helper class///  </summary> public  class Serializablehelper  {    public string Serializable (object target)    {      using (MemoryStream stream = new MemoryStream ())      {        New BinaryFormatter (). Serialize (stream, target);         Return Convert.tobase64string (stream. ToArray ());      }    }     public object Derializable (string target)    {      byte[] Targetarray = convert.frombase64string (target);       using (MemoryStream stream = new MemoryStream (Targetarray))      {        return new BinaryFormatter (). Deserialize (stream);      }    }     Public T derializable<t> (string target)    {      return (T) derializable (target);    }  }

Test:

static void Main (string[] args) {  icolor color = new Redcolor ();  Color. Red = 255;  COLOR.F = new Factroy () {name= "Hubei Factory"};  Console.WriteLine ("Color-factroy:" + color.f.name); Hubei Factory   icolor color1 = color. Clone ();  Color1. Red = 234;  Color1.f.name = "Beijing factory";  Console.WriteLine ("Color1-factroy:" + color1.f.name); Beijing Factory  Console.WriteLine ("Color-factroy:" + color.f.name); Hubei factory  Console.read ();}

The running results of the program are as follows:

Conclusion: New objects are formed by serialization and deserialization. In fact, only if the project is to use the prototype schema for object replication, it can be serialized in the form of deep replication.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we have a lot of topic.alibabacloud.com.

  • Related Article

    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.