zoukankan      html  css  js  c++  java
  • 设计模式(7)--模板模式

    //7.模板模式
    //ver1
    //考试试卷类
    class TestPaper
    {
    public:
    	void TestQuestion1(){}
    	void TestQuestion2(){}
    
    	virtual string Answer1()
    	{
    		return "";
    	}
    	virtual string Answer2()
    	{
    		return "";
    	}
    };
    
    class TestPaperA : public TestPaper
    {
    public:
    	void TestQuestion1()
    	{
    		TestPaper::TestQuestion1();
    		//A回答1
    		Answer1();
    	}
    	void TestQuestion2()
    	{
    		TestPaper::TestQuestion2();
    		//A回答2
    		Answer2();
    	}
    
    	string Answer1()
    	{
    		return "A";
    	}
    	string Answer2()
    	{
    		return "B";
    	}
    };
    class TestPaperB : public TestPaper
    {
    public:
    	void TestQuestion1()
    	{
    		TestPaper::TestQuestion1();
    		//B回答1
    		Answer1();
    	}
    	void TestQuestion2()
    	{
    		TestPaper::TestQuestion2();
    		//B回答2
    		Answer2();
    	}
    	string Answer1()
    	{
    		return "C";
    	}
    	string Answer2()
    	{
    		return "D";
    	}
    };
    
    void main1()
    {
    	TestPaper * pstuA = new TestPaperA();
    	pstuA->TestQuestion1();
    	pstuA->TestQuestion2();
    
    	TestPaper * pstuB = new TestPaperB();
    	pstuB->TestQuestion1();
    	pstuB->TestQuestion2();
    }
    
    //模板模式: 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。
    //模板模式使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
    
    //模板模式通过把不变行为搬到超类,去除子类中的重复代码来体现它的优势。
    

      

  • 相关阅读:
    域环境的搭建 (超级详细)
    sqli-labs(10)
    sqli-labs(9)
    mysql源码安装(5.1)
    查看mysql apache php nginx的编译参数
    LAMP的安装
    Mysql的安装(二进制免编译包) 5.1版本
    更改yum网易 阿里云的yum源。
    yum安裝的包如何保留到本地
    磁盘分区
  • 原文地址:https://www.cnblogs.com/sylar-liang/p/6028817.html
Copyright © 2011-2022 走看看