设计模式(十):模板模式

  此时的模板模式不同于C++中自带的模板泛型。

  模板方法模式——在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。

  下面,是大家很熟悉的一段代码——《Head First 设计模式》中第8章模板方法模式中星巴兹咖啡因饮料代码的C++版。

  

#include <iostream>

using namespace std;

class Coffee
{
public:
    Coffee(){}
    ~Coffee(){}

    void PrepareRecipe()
    {
        BoilWater();
        BrewCoffeeGrinds();
        PourlnCup();
        AddSugarAndMilk();
    }

    void BoilWater()
    {
        cout << "Boil Water." << endl;
    }

    void BrewCoffeeGrinds()
    {
        cout << "Brew Coffee Grinds." << endl;
    }

    void PourlnCup()
    {
        cout << "Pour in Cup." << endl;
    }

    void AddSugarAndMilk()
    {
        cout << "Add sugar and milk." << endl;
    }

    /* data */
};

class Tea
{
public:
    Tea(){}
    ~Tea(){}

    void PrepareRecipe()
    {
        BoilWater();
        SteepTeaBag();
        PourlnCup();
        AddLemon();
    }

    void BoilWater()
    {
        cout << "Boil Water." << endl;
    }

    void SteepTeaBag()
    {
        cout << "Steep Tea Bag." << endl;
    }

    void PourlnCup()
    {
        cout << "Pour in Cup." << endl;
    }

    void AddLemon()
    {
        cout << "Add Lemon." << endl;
    }


    /* data */
};

class CaffeineBeverage
{
public:
    CaffeineBeverage(){}
    ~CaffeineBeverage(){}

    void PrepareRecipe()
    {
        BoilWater();
        Brew();
        PourlnCup();
        AddCondiments();
    }

    void BoilWater()
    {
        cout << "Boil Water." << endl;
    }

    virtual void Brew() = 0;

    void PourlnCup()
    {
        cout << "Pour in Cup." << endl;
    }

    virtual void AddCondiments() = 0;

    /* data */
};

class Coffee1 : public CaffeineBeverage
{
public:
    Coffee1(){}
    ~Coffee1(){}

    void Brew()
    {
        cout << "Brew Coffee Grinds." << endl;
    }

    void AddCondiments()
    {
        cout << "Add sugar and milk." << endl;
    }
};

class Tea1 : public CaffeineBeverage
{
public:
     Tea1(){}
    ~Tea1(){}

    void Brew()
    {
        cout << "Steep Tea Bag." << endl; 
    }

    void AddCondiments()
    {
        cout << "Add Lemon." << endl;
    }

    /* data */
};

template< typename T > class CaffeineBeverage1
{
public:
    CaffeineBeverage1(){}
    ~CaffeineBeverage1(){}

    void PrepareRecipe()
    {
        BoilWater();
        Brew();
        PourlnCup();
        AddCondiments();
    }

    void BoilWater()
    {
        cout << "Boil Water." << endl;
    }

    void Brew()
    {
        static_cast<T*>(this)->Brew();
    }

    void PourlnCup()
    {
        cout << "Pour in Cup." << endl;
    }

    void AddCondiments()
    {
        static_cast<T*>(this)->AddCondiments();
    }
};

class Coffee2 : public CaffeineBeverage1<Coffee2>
{
public:
    Coffee2(){}
    ~Coffee2(){}

    void Brew()
    {
        cout << "Brew Coffee Grinds." << endl;
    }

    void AddCondiments()
    {
        cout << "Add sugar and milk." << endl;
    }


    /* data */
};

class Tea2 : public CaffeineBeverage1<Tea2>
{
public:
    Tea2(){}
    ~Tea2(){}

    void Brew()
    {
        cout << "Steep Tea Bag." << endl; 
    }

    void AddCondiments()
    {
        cout << "Add Lemon." << endl;
    }    

    /* data */
};

int main()
{
    cout << "Give me a cup of coffee." << endl;
    Coffee c;
    c.PrepareRecipe();
    cout << endl;

    cout << "Give me a cup of Tea." << endl;
    Tea t;
    t.PrepareRecipe();
    cout << endl;

    cout << "Give me a cup of coffee." << endl;
    Coffee1 c1;
    c1.PrepareRecipe();
    cout << endl;

    cout << "Give me a cup of Tea." << endl;
    Tea1 t1;
    t1.PrepareRecipe();
    cout << endl;


    cout << "Give me a cup of coffee." << endl;
    Coffee2 c2;
    c2.PrepareRecipe();
    cout << endl;

    cout << "Give me a cup of Tea." << endl;
    Tea2 t2;
    t2.PrepareRecipe();
    cout << endl;


    return 0;
}

 python的代码:

class CaffeineBeverage(object):
    """docstring for CaffeineBeverage"""

    def BoilWater(self):
        print "Boil water."

    def PourInCup(self):
        print "Pour in cup."

    def PrepareRecipe(self):
        self.BoilWater()
        self.Brew()
        self.PourInCup()
        self.AddCondiments()



class Caffee(CaffeineBeverage):
    """docstring for Caffee"""
    
    def Brew(self):
        print "Caffee Brew."

    def AddCondiments(self):
        print "Add milk and sugar."

class Tea(CaffeineBeverage):
    """docstring for Tea"""
    def Brew(self):
        print "Tea brew."

    def AddCondiments(self):
        print "Add milk."
        
        
ca = Caffee()
ca.PrepareRecipe()

te = Tea()
te.PrepareRecipe()
原文地址:https://www.cnblogs.com/bracken/p/3071807.html