Design Mode C ++ implementation (5)-prototype mode and template method mode

Source: Internet
Author: User

The design patterns in the software field provide developers with an effective way to use expert design experience. The design patterns use the important features of object-oriented programming languages: encapsulation, inheritance, and polymorphism. It may be a long process to truly comprehend the essence of the design patterns, which requires a lot of practical experience. I recently read a book on design patterns. I wrote a small example in C ++ for each pattern to help me better understand it. Refer to "big talk Design Patterns" and "design patterns: the basis for reusable object-oriented software" (DP. This article describes the implementation of the prototype mode and template method mode. First introduce the prototype mode, and then introduce the template method mode.

The definition in the DP book is: Use the prototype instance to specify the type of the object to be created, and create a new object by copying the prototype. One word is very important, that is, copying. Copy is the essence of the prototype. Here is an example of a prototype. When looking for a job, we need to prepare our resume. If you do not have a printing device, you need to write your resume. The content of these resumes is the same. This has a defect. If you want to modify an item in your resume, you need to modify all the prepared resumes, which requires a lot of work. As technology advances, printing devices have emerged. We only need to hand-write one copy, and then copy multiple copies using the printing device. If you want to modify an item in your resume, you can modify the original version and copy it again. The original hand-written draft is equivalent to a prototype. With it, you can create more new resumes by copying them. This is the basic idea of the prototype model. The following shows the UML diagram of the prototype mode. The example is used as an example.


The key to implementing the prototype mode is to implement the clone function. For C ++, it is actually a copy constructor and deep copy is required. The following is an implementation.

// Parent class resume {protected: char * Name; public: Resume () {} virtual ~ Resume () {} virtual resume * clone () {return NULL;} virtual void set (char * n) {} virtual void show (){}};
Class resumea: public resume {public: resumea (const char * Str); // constructor resumea (const resumea & R); // copy constructor ~ Resumea (); // destructor resumea * clone (); // clone, the key is void show (); // display content}; resumea :: resumea (const char * Str) {If (STR = NULL) {name = new char [1]; name [0] = '\ 0 ';} else {name = new char [strlen (STR) + 1]; strcpy (name, STR) ;}} resumea ::~ Resumea () {Delete [] Name;} resumea: resumea (const resumea & R) {name = new char [strlen (R. name) + 1]; strcpy (name, R. name);} resumea * resumea: Clone () {return New resumea (* This);} void resumea: Show () {cout <"resumea Name: "<name <Endl ;}

Here only the implementation of resumea is given, and the implementation of resumeb is similar. The method is as follows:

Int main () {resume * R1 = new resumea ("A"); resume * r2 = new resumeb ("B"); resume * R3 = R1-> clone (); resume * r4 = R2-> clone (); R1-> show (); r2-> show (); // Delete R1, r2delete R1; Delete R2; r1 = R2 = NULL; // deep copy does not affect R3 and R4; r3-> show (); R4-> show (); Delete R3; Delete R4; r3 = r4 = NULL ;}

I have a job fair recently. I can bring my resume to apply for a job. However, one of the companies did not accept their resumes. Instead, they sent a short history table containing basic information, education background, work experience, and other columns for the applicants to complete as required. After you get the form, you can enter it. What should I do if I use a program to implement this process? One solution is to use the template method mode: Define the skeleton of an algorithm in an operation, and delay some steps to the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm. In our example, the operation is to fill in the resume process. We can define the algorithm skeleton of the operation in the parent class, and the specific implementation is completed by the Child class. The UML diagram is given below.

Fillresume () defines the skeleton of operations and calls the functions implemented by sub-classes in turn. This is equivalent to the actual process in which each person fills in his or her resume. Then the corresponding C ++ code is provided.

// Resume class resume {protected: // protection member virtual void setpersonalinfo () {} virtual void seteducation () {} virtual void setworkexp () {} public: void fillresume () {setpersonalinfo (); seteducation (); setworkexp () ;}}; class resumea: public resume {protected: void setpersonalinfo () {cout <"A's personalinfo" <Endl;} void seteducation () {cout <"A's Education" <Endl;} void setworkexp () {cout <"A's work experience" <Endl ;}}; class resumeb: public resume {protected: void setpersonalinfo () {cout <"B's personalinfo" <Endl;} void seteducation () {cout <"B's Education" <Endl;} void setworkexp () {cout <"B's Work experience" <Endl ;}};

The usage is as follows:

int main(){Resume *r1;r1 = new ResumeA();r1->FillResume();delete r1;r1 = new ResumeB();r1->FillResume();delete r1;r1 = NULL;return 0;}

I enjoy the copyright of blog articles, reprint please indicate the source http://blog.csdn.net/wuzhekai1985


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.