Design Pattern-prototype Pattern)

Source: Internet
Author: User

1. Overview
Intent:
We use existing objects as prototypes. Users can create new objects by copying these prototypes.
Usage:The prototype mode can be used when a system is created, constructed, and expressed independently of a product. In prototype mode, the clone method of product creation and Initialization is completed. In use, we can use some column prototype objects to generate the factory objects of the corresponding objects, and copy, paste, and other operations can be independent of the objects to be copied.
Structure:
Prototype: Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object. In the prototype mode, another custom object is created from an object without any creation details.

Basic Code of the prototype mode:
Prototype:
Public abstract class prototype
{
Private string ID;

// Constructor
Public prototype (string ID)
{
This. ID = ID;
}

// Property
Public String ID
{
Get {return ID ;}
}

Public abstract prototype clone ();
}

Specific prototype:
Public class concreteprototype1: Prototype
{
// Constructor
Public concreteprototype1 (string ID)
: Base (ID)
{
}

Public override prototype clone ()
{
// Shallow copy
Return (prototype) This. memberwiseclone ();
}
}


Public class concreteprototype2: Prototype
{
// Constructor
Public concreteprototype2 (string ID)
: Base (ID)
{
}

Public override prototype clone ()
{
// Shallow copy
Return (prototype) This. memberwiseclone ();
}
}

Client:
Concreteprototype1 p1 = new concreteprototype1 ("I ");
Concreteprototype1 C1 = (concreteprototype1) p1.clone ();
Console. writeline ("cloned: {0}", c1.id );

Concreteprototype2 P2 = new concreteprototype2 ("II ");
Concreteprototype2 C2 = (concreteprototype2) p2.clone ();
Console. writeline ("cloned: {0}", c2.id );

2. Instance
For. net, prototype is not required in the prototype. in net, the system namespace provides the icloneable interface, which is the only method to clone (), so that we only need to implement this interface to complete the prototype mode.
The following describes the implementation of the resume prototype in the design model:
Code structure:

Resume:
Public class resume: icloneable
{
Private string name;
Private string sex;
Private string age;
Private string timearea;
Private string company;

Public resume (string name)
{
This. Name = Name;
}

// Set personal information
Public void setpersonalinfo (string sex, string age)
{
This. Sex = sex;
This. Age = age;
}
// Set work experience
Public void setworkexperience (string timearea, string Company)
{
This. timearea = timearea;
This. Company = company;
}

// Display
Public void display ()
{
Console. writeline ("{0} {1} {2}", name, sex, age );
Console. writeline ("work experience: {0} {1}", timearea, company );
}

Public object clone ()
{
Return (object) This. memberwiseclone ();
}

}

Client call:
Static void main (string [] ARGs)
{
Resume A = New resume ("laruence ");
A. setpersonalinfo ("male", "29 ");
A. setworkexperience ("1998-2000", "XX Company ");
Resume B = (resume) A. Clone ();
B. setworkexperience ("1998-2006", "YY enterprise ");
Resume c = (resume) A. Clone ();
C. setpersonalinfo ("male", "24 ");
A. Display ();
B. Display ();
C. Display ();
Console. Read ();
}

Result:
Laruence male 29
Work Experience: 1998-2000 XX Company
Laruence 29
Work Experience: 1998-2006 YY Company
Laruence male 24
Work Experience: 1998-2000 XX Company
Generally, cloning is the best method when the initialization information does not change. This not only hides the details of object creation, but also greatly improves the performance.
Next let's look at deep cloning and shortest cloning:
In the preceding resume class, the data is of the string type, while the string type is a special reference type with the value type characteristics. The memberwiseclone () method performs a one-by-one copy of the Value Type field, for the reference type, only the referenced object is copied. Therefore, the original object and its copy reference the same object. Let's take a look at the code implementation of the reference type resume clone below:
Code structure:

Code details:
Work experience:


// Work experience
Public class workexperience
{
Private string workdate;
Public String workdate
{
Get {return workdate ;}
Set {workdate = value ;}
}
Private string company;
Public String Company
{
Get {return company ;}
Set {Company = value ;}
}
}

Resume:
// Resume
Public class resume: icloneable
{
Private string name;
Private string sex;
Private string age;

Private workexperience work;

Public resume (string name)
{
This. Name = Name;
Work = new workexperience ();
}

// Set personal information
Public void setpersonalinfo (string sex, string age)
{
This. Sex = sex;
This. Age = age;
}
// Set work experience
Public void setworkexperience (string workdate, string Company)
{
Work. workdate = workdate;
Work. Company = company;
}

// Display
Public void display ()
{
Console. writeline ("{0} {1} {2}", name, sex, age );
Console. writeline ("work experience: {0} {1}", work. workdate, work. Company );
}

Public object clone ()
{
Return (object) This. memberwiseclone ();
}

}

Client: static void main (string [] ARGs)
{
Resume A = New resume ("laruence ");
A. setpersonalinfo ("male", "29 ");
A. setworkexperience ("1998-2000", "XX Company ");

Resume B = (resume) A. Clone ();
B. setworkexperience ("1998-2006", "YY enterprise ");

Resume c = (resume) A. Clone ();
C. setpersonalinfo ("male", "24 ");
C. setworkexperience ("1998-2003", "ZZ enterprise ");

A. Display ();
B. Display ();
C. Display ();

Console. Read ();
}

The running result is as follows:
Laruence male 29
Work Experience: 1998-2003 ZZ Enterprises
Laruence 29
Work Experience: 1998-2003 ZZ Enterprises
Laruence male 24
Work Experience: 1998-2003 ZZ Enterprises
Because the memberwiseclone () method is a superficial copy (clone), there is no problem with value type cloning. For reference type objects, only references are copied, And the referenced objects still point to the original objects, therefore, I set the 'work experience' for the references a, B, and C, but at the same time, we can see that the three references are the last settings, because all three references point to the same object.
For "shortest copy", 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 copy" means that the variables of the referenced object are directed to the Copied object instead of the original referenced object.
The following describes the implementation of deep replication:
Code structure:


Implementation Code:
Work experience:

// Work experience
Public class workexperience: icloneable
{
Private string workdate;
Public String workdate
{
Get {return workdate ;}
Set {workdate = value ;}
}
Private string company;
Public String Company
{
Get {return company ;}
Set {Company = value ;}
}

Public object clone ()
{
Return (object) This. memberwiseclone ();
}
}

Resume:
// Resume
Public class resume: icloneable
{
Private string name;
Private string sex;
Private string age;

Private workexperience work;

Public resume (string name)
{
This. Name = Name;
Work = new workexperience ();
}

Private resume (workexperience work)
{
This. Work = (workexperience) work. Clone ();
}

// Set personal information
Public void setpersonalinfo (string sex, string age)
{
This. Sex = sex;
This. Age = age;
}
// Set work experience
Public void setworkexperience (string workdate, string Company)
{
Work. workdate = workdate;
Work. Company = company;
}

// Display
Public void display ()
{
Console. writeline ("{0} {1} {2}", name, sex, age );
Console. writeline ("work experience: {0} {1}", work. workdate, work. Company );
}

Public object clone ()
{
Resume OBJ = New resume (this. Work );

OBJ. Name = This. Name;
OBJ. Sex = This. sex;
OBJ. Age = This. Age;


Return OBJ;
}

}

The client code is the same as above. The execution result is as follows:
Laruence male 29
Work Experience: 1998-2000 XX Company
Laruence 29
Work Experience: 1998-2006 YY Enterprise
Laruence male 24
Work Experience: 1998-2003 ZZ Enterprises
3. Summary
Advantages and disadvantages:
The prototype has the following advantages:
(1 ). To add or delete a product at runtime, you only need to register a prototype instance through the client to add the new product type to the system. For example, each tool in the toolbox of the configuration software can correspond to a registered prototype object, you can extend the toolbox by adding a prototype object.
(2 ). It is easy to create complex objects: complex elements are often created in image editing and configuration software. These elements are composed of simple elements, using the prototype mode, you can easily use complex elements as common elements. The toolbox of the software has the extended function.
(3 ). Reduce factory layers: Because reflection factories can be used in. net, this advantage is not obvious.
Disadvantages of the prototype mode: in some cases, the clone function is not easy to implement, especially when the object is referenced cyclically.

Many classes in. net support the prototype mode. For example, we want to obtain a dataset with the same structure as the existing dataset (Dataset), which can be cloned. Note: dataset has two methods: Clone () and copy (). The clone () method is used to copy the dataset structure, but does not copy the data of dataset. This achieves the shortest replication of the prototype mode, the copy () method not only copies the structure, but also copies the data to achieve deep replication in the prototype mode.

Reprinted from: http://www.cnblogs.com/peida/archive/2008/06/26/1230129.html

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.