Design Mode: java
The prototype may not be so popular. In other words, it can be called the copy mode.
From the perspective of copying, there are full copies, and incomplete copies, as if Sun monkey's hair-blowing monkeys, but these monkeys are obviously not as powerful as Sun Wukong's Ontology. This copy can be considered a shallow copy.
Since there is a small copy, it must also be a deep copy. Deep copy means that the monkey and Sun Wukong are as powerful as the ontology, both in terms of skill and appearance.
I. Prototype
/// <Summary> /// Prototype /// </summary> abstract public class Prototype {public int Id {get; set;} public string Name {get; set ;} public List <int> Size = new List <int> (); public Prototype (int id) {this. id = id;} abstract public Prototype Clone ();}
On the prototype, I put a value type, a string, and an integer set. In the prototype mode, I will modify these values to observe the changes after cloning.
Ii. 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 = new Concrete1 (1); p1.Name = "Name"; p1.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 (); p1.Id = 2; p1.Name = "p1.Name"; p1.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 ));
Result:
As you can see, after cloning, I modified the value of the prototype set, and the cloned object also changed, indicating that their set variables point to the same heap space.
Console.WriteLine("p1.Size == c1.Size, {0}", p1.Size == c1.Size);
A picture has a truth.
Iii. 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 = new Concrete2 (2); p2.Name = "Name"; p2.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 (); p2.Id = 2; p2.Name = "p2.Name"; p2.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 );
Result:
As you can see, the copy object has not changed, indicating that they are already two independent and separate entities, and there is no common variable reference (except for method reference ).
Here, I implement the deep COPY method through serialization. Of course there are many other methods. The clone method provided by. C # can be implemented by myself, which is the implementation of the shortest copy.
C # There is an interface: ICloneable. If you do not want to make it into an abstract class, you can also implement it through this interface.
Iv. Personal applications
In my personal application in the project, I recently used structure cloning.
The Datatble variable, whether B/s or c/s, should have been used.
It has a Clone method that can be used to Clone table structures and constraints, which is quite convenient. I don't need to create complicated table structures again.
Refer:
Big talk Design Model
C # design mode (9)