Design Pattern essay-prototype Pattern

Source: Internet
Author: User
The following is what I learned today. I read the book "big talk Design Pattern". Many of the Code is the code in the book, but it is my own understanding. Remember it as a learning note.
 
Today we are learning the prototype:

The class diagram is as follows:
--- The image is not uploaded --!

For example, to design a resume class, we have initially defined the structure of this class. When the client calls this class, we need to generate an instance and then call the methods in this class. In this way, only one resume is processed. If there are hundreds of resumes, do we still need to go to the resumes for so many instances? That way... Of course, we can call the same resume in a loop (although not a good method) or assign an instance to other instances, but the results are the same, each person's resume is the same and plagiarized. The following prototype shows us a method of plagiarism :. net provides us with a method: MemberwiseClone () This method is used to perform shallow replication for members in the class (and so on to introduce what is a shallow replication ). The Code is as follows:

Prototype
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace resume
{
///
/// Prototype
///
Abstract class Prototype
{
Private string id;
Public Prototype (string id)
{
This. id = id;
}
Public string ID
{
Get {return id ;}
}
Public abstract Prototype Clone ();
}
}

The above is a basic prototype and serves as a copy template;

 

Code

Namespace resume
{
/// <Summary>
/// Specific prototype
/// </Summary>
Class ConcretePrototype1: Prototype
{
Public ConcretePrototype1 (string id)
: Base (id)
{
 
}
Public override Prototype Clone ()
{
Return (Prototype) this. MemberwiseClone ();
// Throw new NotImplementedException ();
}
}
}

The above is a prototype and the Clone method is implemented.
The following is the client code:

ConcretePrototype1 p1 = new ConcretePrototype1 ("123456789 ");
ConcretePrototype1 c1 = (ConcretePrototype1) p1.Clone ();
Console. WriteLine ("Cloned: {0}", c1.ID );

The above completes the clone of a class member.
. Net provides another interface to implement the above functions: ICloneableclass

Code
Namespace resume
{
/// <Summary>
/// Resume code
/// </Summary>
Class Resume: ICloneable
{
Private string name;
Private string sex;
Private int age;

Public Resume (string name)
{
This. name = name;
}
Public void SetPersonInfo (string sex, int age)
{
This. sex = sex;
This. age = age;
}
Public void Display ()
{
Console. WriteLine ("{0} {1} {2}", name, sex, age );
}
/// <Summary>
/// Implement the Clone Interface
/// </Summary>
/// <Returns> </returns>
Public object Clone ()
{
Return (object) this. MemberwiseClone ();
}
}
}

The client can call the following code:

Code
Resume resume = new Resume ("ggg ");
Resume. SetPersonInfo ("m", 22 );
Resume bb = (Resume) a. Clone ();
Bb. SetPersonInfo ("f", 23 );
Resume. Display ();
Bb. Display ();

In this way, you can copy the resumes of multiple people on the premise that they are proposed from a template. Is this much more convenient?
As mentioned above, the MemberwiseClone () method is like this,If the field is of the value type, perform a one-on-one copy of the field. If the field is of the reference type, copy the reference ticket and do not copy the used 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.