Prototype mode (PROTOTYPE)
Class diagram
Describe
Prototype mode:
Provides an interface--clone method for cloning itself.
Application Scenarios
Defines a student class that contains the age property of a value type (int), the two reference type name (string), and the course property.
Public classCourse { Public stringName {Get;Set; } } Public classstudent:icloneable { Public stringName {Get;Set; } Public intAge {Get;Set; } PublicCourse Course {Get;Set; } Public ObjectClone () {return This. MemberwiseClone (); } } Static voidMain (string[] args) { stringValue = configurationmanager.appsettings["Prototypepattern"]; Student Student= (Student) assembly.load (value. Substring (0, value. IndexOf ('.'))). CreateInstance (value); Student. Name="Jim"; Student. Age= -; Student. Course=NewCourse () {Name ="C + +" }; Console.WriteLine ("Student:"+ Student. Name +"\ t"+ Student. Age +"\ t"+student. Course.name); Console.WriteLine (); Student Student1=(Student) Student. Clone (); Student1. Name="Tom"; Student1. Age= +; Student1. Course.name="C #"; Console.WriteLine ("Student:"+ Student. Name +"\ t"+ Student. Age +"\ t"+student. Course.name); Console.WriteLine ("Student1:"+ Student1. Name +"\ t"+ Student1. Age +"\ t"+Student1. Course.name); Console.WriteLine (); Student1. Course.name="Java"; Console.WriteLine ("Student:"+ Student. Name +"\ t"+ Student. Age +"\ t"+student. Course.name); Console.WriteLine ("Student1:"+ Student1. Name +"\ t"+ Student1. Age +"\ t"+Student1. Course.name); }
Output:
Student:Jim C + +
Student:Jim C #
Student1:tom C #
Student:Jim Java
Student1:tom Java
From the printed results can be seen, to the original object student copy a new Student1 object and assign values to the Student1 property, the original object student the age and name unchanged, Coursename has changed. The reason is that after the copy, student the Student1 's age and name, and Student1 's course still points to student's course address, so when Student1 's coursename changes, Student's coursename also changed, which is a shallow copy.
However, in practical applications, it is often not desirable to have such a thing, that is, the copy object can not be changed to affect the original object. The reason is simple, not because Tom chose C # This course requires Jim must also change the selected C + + course to C #. To solve this problem, you need to copy a completely new object, deep copy.
Let's change the student class and the course Class a little bit, and create a new student by serializing and deserializing:
[Serializable] Public classCourse { Public stringName {Get;Set; } } [Serializable] Public classstudent:icloneable { Public stringName {Get;Set; } Public intAge {Get;Set; } PublicCourse Course {Get;Set; } Public ObjectClone () {using(Stream Objectstream =NewMemoryStream ()) {IFormatter Formatter=NewBinaryFormatter (); Formatter. Serialize (Objectstream, This); Objectstream.seek (0, Seekorigin.begin); returnFormatter. Deserialize (Objectstream); } } }
Output:
Student:Jim C + +
Student:Jim C + +
Student1:tom C #
Student:Jim C + +
Student1:tom Java
The difference between a shallow copy and a deep copy:
Shallow copy: A direct copy of a value type and a string type, a common pointer to a reference type, and a coupling between two objects;
Deep copy: Copies an entirely new object to the object, with a coupling degree of 0 between the two objects.
Design Pattern Notes-prototype mode (PROTOTYPE)