Design mode: Prototype mode

Source: Internet
Author: User

Prototype mode: Prototypepattern

By using an existing object, the clone generates a completely new object, based on which it modifies some content to form a new object entity.

The real key point is a cloned object (as well as a deep, shallow clone) that can be quickly understood by code:

Namespace Core
{
<summary>
By using an existing object, the clone generates a completely new object, based on which it modifies some content to form a new object entity.
1. Object construction method does not execute when cloning object
2. Latent and deep reproduction:
Shallow copy: The Clone method of the object class simply copies the original data type of this object, such as int,float,string, and is not copied for references to arrays and objects.
Deep copy: Copy behavior for objects and arrays
</summary>
public class daylife:icloneable//Cloning interface icloneable
{
private string name;

public string Name
{
get {return name;}
set {name = value;}
}
public void work ()
{
Console.Write (this.name+ "to work \ \");
}
public void GoHome ()
{
Console.Write (this.name+ "off duty \");
}


Implementing an interface Cloning method
public Object Clone ()
{
Call the MemberwiseClone () of the object parent class
Return base. MemberwiseClone ();
}
}
}

The icloneable interface has a clone () method that invokes a MemberwiseClone () under the common parent class object of the. Net framework to complete the cloning of subclasses;

Define a factory (code of Conduct):

Namespace Ifactory
{
Using Core;
public interface Idaylife
{
Daylife getinstance ();
}
}

Implement this interface:

Namespace Factory
{
Using Ifactory;
Using Core;
public class Daylifefactory:idaylife
{
A static instance object
public static Daylife dl = NULL;
Public Core.daylife getinstance ()
{
if (DL = = null)
{
Console.Write ("new\n");
Dl=new Daylife ();
return DL;
}
Else
{
Console.Write ("clone\n");
Return (Daylife) DL. Clone ();
}
}
}
}

The Daylife type object must be static, the first time the object is acquired with new, and when the second begins, a clone of the prototype pattern is used.

idaylife FAC = new Daylifefactory ();
           //First time: Create Object
             Daylife DAYLIFE=FAC. GetInstance ();
            Daylife. Name = "Francis";
            Daylife. Work ();
            Daylife. GoHome ();
           //Partial modification: Cloning
             Daylife = FAC. GetInstance ();
            Daylife. Name = "Lyfeng";
            Daylife. GoHome ();
            Daylife. Work ();

Here it is necessary to continue to present a shallow, deep copy of the concept:

Shallow copy: The Clone method of the object class simply copies the original data type of this object, such as int,float,string, and is not copied for references to arrays and objects.

Deep copy: Copy behavior for objects and arrays

To verify, create a new test class

public class Test:icloneable
{
Public ArrayList list = new ArrayList ();
public void Set (string s)
{
List. ADD (s);
}
public string get (int i)
{
Return List[i]. ToString ();
}

public Object Clone ()
{
Shallow copy
Return base. MemberwiseClone ();
Deep clones
Test T = (test) base. MemberwiseClone ();
T.list = (ArrayList) list. Clone ();
return t;
}
}

Call in Client://Verify the difference between shallow copy and deep copy
Test T = new Test ();
T.set ("one");
T.set ("both");
for (int i = 0; i < T.list.count; i++)
{
Console.Write ("T:" + t.list[i] + "");
}
Test T1 = (test) t.clone ();
T1.set ("four");
Console.WriteLine ();
for (int i = 0; i < T1.list.Count; i++)
{
Console.Write ("T1:" + t1.list[i] + "");
}
Console.WriteLine ();
for (int i = 0; i < T.list.count; i++)
{
Console.Write ("T:" + t.list[i] + "");
}

Shallow cloning method, will change the original object and array, deep cloning will not;

Use occasions :

    1. 1. the process of generating objects is more complex , when initialization requires a lot of resources
    2. 2. When you want the frame prototype to be separated from the resulting object
    3. 3. the same object may be invoked concurrently by other callers

prototype mode general and Factory mode use together , Factory Complex provides a cloned object to the caller of the application .

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.