Design pattern C++ implementation —— prototype pattern, template method pattern

Posted by mashnote on Thu, 09 May 2019 22:30:03 +0200

Design patterns in the field of software provide an effective way for developers to use expert design experience. Design patterns utilize the important features of object-oriented programming language: encapsulation, inheritance, polymorphism, a true understanding of the essence of design patterns is a potentially lengthy process that requires a lot of practical experience.Recently, I read the book of design patterns.For each pattern, I write a small example with C++ to deep my understanding. This paper main refers to Dahua Design Patterns and Design Patterns: The Foundation of Reusable Object-Oriented Software (DP). This paper introduces the implementation of prototype and template method. First, the prototype is introduced, and then the TEmplate method mode is introduced.

The definition in DP book is to specify the types of objects created with prototype instances and create new objects by copying these prototypes. One of the important words is copy. It can be said that copy is the essence of the prototype model. Take a real example to introduce the prototype model. When looking for a job, we need to prepare a resume. Assuming there is no printing device, you need to write your resume by hand. The content of these resumes is the same. This has a flaw, if you want to modify one of the resumes, then all the resumes have been written to modify, a lot of work. With the progress of science and technology, printing equipment has emerged. We just need to write one by hand, and then use the printing equipment to copy more copies. If you want to modify an item in your resume, you can modify the original version and then copy it. The original manuscript is equivalent to a prototype, with which more new resumes can be created by copying (copying). This is the basic idea of the prototype model. The UML diagram of the prototype pattern is given below, taking the example just given as an example.

The key to implement prototype mode is to implement Clone function. For C++, it is actually a copy constructor, which needs deep copy. Here is an implementation.

//Parent class
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();          //Cloning, the key
    void Show();               //show contents
};
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; 
}


The implementation of ResumeA is given here, and the implementation of ResumeB is similar. The following methods are used:

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,r2
    delete r1; delete r2;    
    r1 = r2 = NULL;
    //Deep copies therefore have no effect on r3,r4
    r3->Show(); r4->Show();
    delete r3; delete r4;
    r3 = r4 = NULL;
}


Recently, there is a job fair where you can bring your resume to apply for a job. However, one of the companies did not accept resumes, but sent a resume form with basic information, education background, work experience and so on, so that the applicants could fill in the form as required. Everyone gets this form and starts filling it out. What if this process is programmed? One solution is to use the template method pattern: define the skeleton of an algorithm in an operation, and defer some steps to subclasses. Template method enables subclasses to redefine some specific steps of an algorithm without changing its structure. In our example, operations are the process of filling out resumes. We can define the algorithm skeleton of operations in the parent class, and the specific implementation is done by the subclass. Its UML diagram is given below.

FillResume() defines the skeleton of operations and calls functions implemented by subclasses in turn. It's equivalent to the actual process of filling out a resume for everyone. Then the corresponding C++ code is given.

//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 method of use 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;
}


Thank you for sharing!
Original address: https://blog.csdn.net/wuzhekai 1985/column/info/design

Topics: Programming