Big talk design model-prototype model (learning notes)

Source: Internet
Author: User

Prototype: specifies the type of the object to be created using the prototype instance, and creates a new object by copying the prototype.
In prototype mode, another custom object is created from an object without any details.
Prototype pattern structure:

Basic Prototype code: Prototype class abstract class Prototype
{
Private string id;


Public Prototype (string id ){
This. id = id;
}


Public string Id {
Get {return id ;}
}


Public abstract Prototype Clone (); <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Cipher + Cjxicj4Kvt/cipher + cipher/OwurXEt8e + cipher/Oju9LytMujrNStyry21M/cipher + cipher = "I ");
ConcretePrototype1 c1 = (ConcretePrototype1) p1.Clone (); // clone class to get the current object.
Console. WriteLine ("Coloned: {0}", p1.Id );
Console. WriteLine ("Coloned: {0}", c1.Id );
Console. Read ()
Detailed instance (deep copy and light copy) Shortest structure:


Deep copy Structure




Code: // defines a Clone method to Clone itself
// Shallow replication does not inherit the ICloneable interface or implement the Clone method.
Class WorkExperence: ICloneable {
Private string workDate;
Private string company;
Public string WorkDate {
Set {workDate = value ;}
Get {return workDate ;}
}
Public string Company {
Set {company = value ;}
Get {return company ;}
}
Public Object Clone (){
Return (Object) this. MemberwiseClone ();
}


}

// Inherit from the ICloneable interface (in the system namespace)
Class Resume: ICloneable
{
Private string name;
Private string age;
Private string sex;


Private WorkExperence work;


Public Resume (string name ){
This. name = name;
Work = new WorkExperence ();
}
// Implement the constructor for deep replication of resource objects
Public Resume (WorkExperence work ){
This. work = (WorkExperence) work. Clone ();
}


Public void SetPersonalInfo (string sex, string age ){
This. sex = sex;
This. age = age;


}


Public void SetWorkExperence (string workDate, string company ){
Work. WorkDate = workDate;
Work. Company = company;
}


Public void Display (){
Console. WriteLine ("{0} {1} {1}", name, sex, age );
Console. WriteLine ("work experience: {0} {1}", work. WorkDate, work. Company );
}
// Light copy
Public Object Clone (){
Return (Object) this. MemberwiseClone ();
}
// Deep copy
Public Object CloneH () {// call the private constructor to clone the "work experience", assign values to the relevant fields of the "resume" Object, and finally return a deep copy Object
Resume obj = new Resume (this. work );
Obj. name = this. name;
Obj. sex = this. sex;
Obj. age = this. age;
Return obj;
}
}
Front-end code:
Static void Main (string [] args)
{
Resume r1 = new Resume ("character ");


R1.SetPersonalInfo ("male", "25 ");
R1.SetWorkExperence ("5", "marchsoft ");


Resume r2 = (Resume) r1.Clone ();


R2.SetPersonalInfo ("female", "22 ");
R2.SetWorkExperence ("2", "sun ");


Resume r3 = (Resume) r2.CloneH ();
R3.SetWorkExperence ("10", "IBM ");
R3.SetPersonalInfo ("male", "35 ");


R1.Display ();
R2.Display ();
R3.Display ();



Console. Read ();


}

Result

Summary:
In light replication, all variables of the Copied object contain the same value as the original object, and all references to other objects still point to the original object.
Deep replication: only the variables of the referenced object need to be copied to the new object, instead of the original referenced object.
Advantage: Generally, cloning is the best solution when the initialization information does not change. This hides the details of object creation and greatly improves the performance. It is equivalent to the dynamic or side object running state without re-initializing the object.


?


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.