zoukankan      html  css  js  c++  java
  • 设计模式(十):模板模式

      此时的模板模式不同于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()
  • 相关阅读:
    Visual Studio 进行单元测试时如何附加被测试文件的方法总结
    PowerDesigner实体模型CDM中关于建立Entity之间关系的备忘
    【转帖】C# 与 C++ 数据类型对照
    【转帖】解决继承窗体或用户控件时“visual继承当前被禁用,因为基类引用设备特定的组件或包含 p/invoke”问题
    【Winform窗体控件开发】之五 实现类型转换器TypeConverterAttribute
    SQL 使用CONVERT函数 格式化日期
    【转帖】const 与 readonly 的区别
    【转帖】C#与C Windows API数据类型对应关系
    【.Net Compact Framework开发】 使用 Visual Studio 对移动项目进行Unit Testing的方法总结
    【部署】Visual Studio 2008 打包部署.Net Framework 2.0 应用程序提示需要安装.Net Framework 3.5的解决方法
  • 原文地址:https://www.cnblogs.com/bracken/p/3071807.html
Copyright © 2011-2022 走看看