• 设计模式C++实现——模板方法模式


    模式定义:

            模板方法模式在一个方法中定义了一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类能够在不改变算法结构的情况下,又一次定义算法中的某些步骤。

            模板就是一个方法。更详细的说。这种方法将算法定义成一组步骤。当中的不论什么步骤都能够是抽象的,由子类实现。

    这能够确保算法的结果保持不变,同一时候由子类提供部分实现。

    模式结构:

    举例:

            泡咖啡和泡茶步骤与基本同样,定义咖啡和茶的类例如以下:

    class Coffee
    {
    public:
    	void prepareRecipe()
    	{
    		boilWater();
    		brewCoffeeGrinds();
    		pourInCup();
    		addSugarAndMilk();
    	}
    
    	void boilWater()
    	{
    		cout << "Boiling water" << endl;
    	}
    
    	void brewCoffeeGrinds()
    	{
    		cout << "Dripping Coffee through filter" << endl;
    	}
    
    	void pourCup()
    	{
    		cout << "Pouring into cup" <<endl;
    	}
    
    	void addSugarAndMilk()
    	{
    		cout << "Adding Sugar and Milk" << endl;
    	}
    };
    
    class Tea
    {
    public:
    	void prepareRecipe()
    	{
    		boilWater();
    		brewReaBag();
    		pourInCup();
    		addLemon();
    	}
    
    	void boilWater()
    	{
    		cout << "Boiling water" << endl;
    	}
    
    	void brewReaBag()
    	{
    		cout << "Steeping the tea" << endl;
    	}
    
    	void pourCup()
    	{
    		cout << "Pouring into cup" <<endl;
    	}
    
    	void addLemon()
    	{
    		cout << "Adding Lemon" << endl;
    	}
    };
    


            可见有两份冲泡算法中都採用了把水煮沸和把饮料倒入杯子的算法,所以能够把他们放到Coffee和Tea的基类(新定义一个咖啡因类CaffeineBeverage.)中。

    还有两个算法并没有被放入基类,但能够将他们定义新的方法名称brew()和addCondiments()方法,并在子类中实现。

    UML设计:

     

    编程实现及运行结果:

    #include <iostream>
    
    using namespace std;
    
    //定义咖啡因基类
    class CaffeineBeverage
    {
    public:
    	void prepareRecipe()
    	{
    		boilWater();
    		brew();
    		pourInCup();
    		addCondiments();
    	}
    
    	void boilWater()
    	{
    		cout << "Boiling water" << endl;
    	}
    
    	void pourInCup()
    	{
    		cout << "Pouring into cup" <<endl;
    	}
    
    	virtual void brew(){}
    
    	virtual void addCondiments(){}
    };
    //定义咖啡类
    class Coffee : public CaffeineBeverage
    {
    public:
    	void brew()
    	{
    		cout << "Dripping Coffee through filter" << endl;
    	}
    
    	void addCondiments()
    	{
    		cout << "Adding Sugar and Milk" << endl;
    	}
    };
    //定义茶类
    class Tea : public CaffeineBeverage
    {
    public:
    	void brew()
    	{
    		cout << "Steeping the tea" << endl;
    	}
    
    	void addCondiments()
    	{
    		cout << "Adding Lemon" << endl;
    	}
    };
    //客户代码
    int main()
    {
    	Coffee coffee;
    	cout << "Making coffee..." << endl;
    	coffee.prepareRecipe();
    	cout << endl << endl;
    
    	Tea tea;
    	cout << "Make tea...";
    	tea.prepareRecipe();
    	return 0;
    }


            运行结果例如以下:

    Makingcoffee...

    Boilingwater

    DrippingCoffee through filter

    Pouringinto cup

    AddingSugar and Milk

     

     

    Maketea...Boiling water

    Steepingthe tea

    Pouringinto cup

    AddingLemon

    请按随意键继续. . .

    设计原则的应用:

            好莱坞原则:别调用(打电话)我们,我们会调用你。在好莱坞原则下,我们同意低层组件将自己挂钩到系统上。可是高层组件会决定什么时候和怎么样使用这些低层组件。如在模板方法中:当我们设计模板方法模式时,我们告诉子类。“不要调用我们。我们会调用你”。

  • 相关阅读:
    [BZOJ3172]单词
    [BZOJ2434]阿狸的打字机
    [BZOJ1195]最短母串
    [codeforces743E]Vladik and cards
    [BZOJ2553]禁忌
    [BZOJ1009]GT考试
    [BZOJ3507]通配符匹配
    [BZOJ4027]兔子与樱花
    test20190308
    Luogu P2742 模板-二维凸包
  • 原文地址:https://www.cnblogs.com/lytwajue/p/6698837.html
Copyright © 2020-2023  润新知