Template pattern of c + +

Template mode

Function: defines the skeleton of an algorithm in an operation. The template method can redefine some specific steps of an algorithm without changing the structure of the algorithm. The key is to encapsulate the general algorithm (logic) in the abstract base class, and implement different algorithm details in the subclass.

In my opinion, the advantage of template pattern is that it can reduce the code segment reuse, encapsulate the public behavior into the base class, and write the implementation function of the behavior process in the base class. In this way, for different subclasses in different situations, you can modify or rewrite the function in the subclass, and the subclass does not need to worry about the specific process.

Advantages: 1. Package invariant part and expand changeable part.

2. Extract common code for easy maintenance.

3. The behavior is controlled by the parent class and implemented by the child class.

Disadvantages: each different implementation needs a subclass, which leads to the increase of the number of classes and makes the system larger.

If a developer wants to build three houses, some steps in the process of building each house are fixed, such as building the foundation first, then building the wall, then decorating the wall, and then interior decoration. But although the steps are the same, different houses may adopt different architectural styles. Let's write a program to realize this process

If the template mode is not used, each house is a class, and the specific details of building house steps in each class may be different, so different implementations should be written in the same function, and the code is as follows:

 

#include <iostream>
using namespace std;
class House1
{
public:
    void foundation()
    {
        cout<<"1.Lay the foundation"<<endl;;
    }
    void wall()
    {
        cout<<"2.Wall house 1 as per drawing 1"<<endl;;
    }
    void color()
    {
        cout<<"3.Whitewashed walls"<<endl;;
    }
    void decorte()
    {
        cout<<"4.Interior decoration of European and American style"<<endl;;
    }
};

class House2
{
public:
    void foundation()
    {
        cout<<"1.Lay the foundation";
    }
    void  wall()
    {
        cout<<"2.Wall house 2 as per drawing 2"<<endl;;
    }
    void color()
    {
        cout<<"3.Walls painted green"<<endl;;
    }
   void decorte()
    {
        cout<<"4.Interior decoration of ancient style"<<endl;
    }
};

class House3
{
public:
    void foundation()
    {
        cout<<"1.Lay the foundation"<<endl;;
    }
    void wall()
    {
        cout<<"2.Wall house 3 as per drawing 3"<<endl;;
    }
    void color()
    {
        cout<<"3.Whitewashed walls"<<endl;;
    }
    void decorte()
    {
        cout<<"4.Carry out Gothic interior decoration"<<endl;;
    }
};
int main()

{
    House1 a1;
    a1.foundation();
    a1. wall();
    a1.color();
    a1.decorte();
    cout<<"================================================="<<endl;
    House2 a2;
    a2.foundation();
    a2. wall();
    a2.color();
    a2.decorte();
    House3 a3;
    cout<<"================================================="<<endl;
    a3.foundation();
    a3. wall();
    a1.color();
    a3.decorte();

}

 

  

 

This kind of writing method not only has a large amount of code, but also requires every different object to call the step flow once in the main function. Moreover, if it is required to paint the fireproof paint once before painting the wall paint, it is very troublesome to add functions to each class when modifying. If the template mode is used to write, these problems can be solved well, and the public The function and part of its implementation are defined in the base class, and then the process is encapsulated in a function. Different subclasses directly rewrite different functions after the base class. For the demand of fireproof paint, only the process function is defined and added in the base class, and the subclass will automatically execute when calling the process function. The specific code is as follows:

#include <iostream>

using namespace std;

class Base
{
protected:
    void foundation()
    {
        cout<<"1.Lay the foundation"<<endl;
    }
    void ex()
    {
        cout<<"Fire retardant paint"<<endl;
    }
public:
    virtual void wall()=0;
    virtual void color()=0;
    virtual void decorte()=0;
    void run()
    {
        foundation();

        wall();

        ex();

        color();

        decorte();
    }
};

class House1:public Base
{
public:
   void wall()
    {
        cout<<"2.Wall house 1 as per drawing 1"<<endl;;
    }
    void color()
    {
        cout<<"3.Whitewashed walls"<<endl;;
    }
    void decorte()
    {
        cout<<"4.Interior decoration of European and American style"<<endl;;
    }
};

class House2:public Base
{
public:
   void  wall()
    {
        cout<<"2.Wall house 2 as per drawing 2"<<endl;;
    }
    void color()
    {
        cout<<"3.Walls painted green"<<endl;;
    }
    void decorte()

    {
        cout<<"4.Interior decoration of ancient style"<<endl;
    }

};

class House3:public Base
{
public:
    void wall()

    {
        cout<<"2.Wall house 3 as per drawing 3"<<endl;;
    }
    void color()
    {
       cout<<"3.Whitewashed walls"<<endl;;
    }
    void decorte()
    {
        cout<<"4.Carry out Gothic interior decoration"<<endl;;
    }
};

int main()
{
    Base *a=new House1;
    a->run();
    cout<<"================================================="<<endl;
    a=new House2;
    a->run();
    cout<<"================================================="<<endl;
    a=new House3;
    a->run();
}

Tags: C++

Posted on Thu, 12 Mar 2020 09:22:04 -0400 by eyeself