Design mode C # implementation (4) ---- Prototype

zhaozj2021-02-16  57

The intent of this mode is to specify the type of object to create an object with a prototype instance and create a new object by copying these prototypes. Then first we should already have an object, and this object also supports self-replication (Cologne). In FCL, we know that there is an interface to specifically specify such a contract, that is, the Icloneable interface, only one method clone, the following MSDN's method for the interface in the interface: Create a new object as the current instance copy . Clone can be achieved as a deep copy, or as a shallow copy copy. In deep copies, all objects are repeated; in a shallow copy, only top-level objects are repeated, and the objects below the top level include references. The result cloning must have the same type as the original example or the compatible type of the original instance. This way if we want a self-copy object we can declare a class Myclass such inheritance interface icloneable.

Let's take an example of showing a life to illustrate this method is common. I think we may know the cells, and cells are the minimum unit of constituent body (may not be), we know any (perhaps part) animal or plants are from a cell division, so we can understand the cells is a kind of There is an object of the Clone method, and the cell can be continuously called this method is that our body tends to be complete. You can also give an example is a virus. We have experienced SARS. I think if SARS has no self-replication function, it is impossible to become a climate, and there is a man who has to achieve, it is money (I don't know if it is appropriate), We can make 1 dollars into 2 pieces through our efforts ... Of course, each person's implementation method is different (do not know if it is appropriate). Of course, the first two examples of the above should be a deep copy, because once he is copied is a separate individual, in memory is different two addresses, and the latent table copy is different, the surface is two It is actually two references to an object, that is, they actually exist in the same address, if we change one of the other changes will also change. We can see how the key to applying this model is how to implement Clone methods.

Below is the structural diagram of Prototype (from Rose2003):

Difference between shallow copy and deep copy: Shallow copy refers to copying a field of numeric types in an object to a new object, and the reference field in the object refers to a reference to the target object. If you change the value of the reference field in the target object, he will reflect in the original object, that is, the corresponding fields in the original object will also change. Deep copy and shallow copy are different are referenced by reference, and the deep copy will create a new object in the new object. It is the field that is the same (content of content) in the object, which means this reference and original object. Quote is different, we do not affect the contents of the corresponding fields in the original object when changing this field in the new object. Therefore, there are different two processing methods for prototype mode: shallow copy and deep copy of the object. There is a shallow copy method under the System Namespace in the FCL: MEMBERWISECLONE is a shallow copy of the current Object. In our example I want to achieve a shallow copy at the same time, it can achieve a deep copy, so you can deepen it.

Here is a shallow copy example:

Using system;

Namespace prototype_shallow {

// Because we already have such an interface in the FCL, we don't define new prototype.

Public class concreteprototype1: icloneable {

PRIVATE INT M_ID;

Public int id {

Get {

Return THIS.M_ID;

}

}

Public contteprototype1 (int id) {

THIS.M_ID = ID;

}

Public Object Clone () {

Return this.memberwiseclone ();

}

}

Public class concreteprototype2: iCloneable {

PRIVATE INT M_ID;

Public Int ID

{

get

{

Return THIS.M_ID;

}

}

Public contRtePrototype2 (int ID) {

THIS.M_ID = ID;

}

Public Object Clone () {

Return this.memberwiseclone ();

}

}

}

The specific prototype inherits the interface Icloneable, and the only method clone in this interface is also implemented. We can create objects in the client ConcretePrototype1 P1 = New ConcretePrototype1 (1); ConcretePrototype1 C1 = (ConcretePrototype1) p1.clone (); First we have created object P1, next we have obtained object C1 with the Cologne method through P1, This is a shallow copy (because MEMBERWISECLONE is a shallow copy).

Next, we will have a deep copy. This relatively shallow copy is more difficult. We will not only copy his numeric fields while copying the object, but also copy its reference field (sometimes this kind of work is Very difficult maybe it is impossible).

Namespace prototype_deep {

Using system.collections;

Public Class Concreteprototype: iCloneable

{

PRIVATE INT M_ID;

Public Int ID

{

get

{

Return THIS.M_ID;

}

}

Private arraylist m_ArrayList = new arraylist ();

Public ConcretePrototype (INT ID)

{

THIS.M_ID = ID;

THIS.M_ARRAYLIST.ADD ("FirstObject");

THIS.M_ARRAYLIST.ADD ("SecondObject");

// ...

}

Public Object Clone ()

{

Concreteprototype C = New ConcretePrototype (this.id);

C.M_ArrayList = new arraylist ();

C.M_ArrayList.Add ("FirstObject");

C.M_ARRAYLIST.ADD ("SecondObject");

Return C;

}

Public concreteprototype deepclone () {

Return (ConcretePrototype) this.clone ();

}

}

}

The code shows how to implement a deep copy, the principle of deep copy is the reference to the referenced field you need new (New Thinking before you can use a certain creation mode implementation in front, this is a good habit) One comes out, then copy the object in this field, so it has been prone to loop copy, so the deep copy is more difficult than the shallow copy. Clients can pass ConcretePrototype P = New ConcretePrototype (1); ConcretePrototype C = P.deepclone (); to achieve a new object. code show as below:

ConcretePrototype P = New ConcretePrototype (1); concreteprototype c = p.dePclone ();

This.RichtextBox1.AppendText (p.toString () ":" p.id.tostring () "/ n");

THIS.RICHTextBox1.AppendText (C.TOString () ":" C.Id.toTString () "/ n");

C.M_ArrayList [0] = "Changed";

For (int i = 0; i <= 1; i ) {

this.richtextbox1.appendtext (c.m_arraylist [i] .tostring ());

}

This.RichtextBox1.AppendText ("/ n");

For (int i = 0; i <= 1; i ) {

This.RichtextBox1.AppendText (p.m_arraylist [i] .tostring ());

}

This way we will see that the original object is constant when we change the object of Cologne. In front of the shallow copy we have this comparison, interested readers can try it, most of the Cologne in the FCL is a shallow copy, we can see the class that realizes the Icloneable interface, almost all are shallow copies. By the way, it is possible to use this clone method in the workflow, and other related objects. We can use this Clone method. This can be found in the CRM's Workflow, and many of them implement this method.

I hope that my article can help you understand the prototype in the design mode. About detailed information in this mode You can see the GoF's book is very clear. In this implementation we use the FCL interface, no write interface but the effect is the same. If there is any place where there is any place in the article corrects: wu_jian830@hotmail.com Thank you for your support, I hope we can make progress together.

转载请注明原文地址:https://www.9cbs.com/read-23926.html

New Post(0)