zoukankan      html  css  js  c++  java
  • 设计模式之—模板方法模式<Templete Method pattern>

    模板方法模式:定义一个操作中的算法骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。

    模板方法模式是通过把不变的行为搬移到超类,去除子类中重复的代码来体现它的优势。提供一个很好的代码复用平台。

    以考试试题为例:不同的只有选项

    试卷类(TestPaper):(注释的代码是另外的实现方式。均为模板方法模式)

    namespace Templete_Method_Pattern
    {
        class StudentA:TestPaper
        {
            //public override string AnswerA()
            //{
            //    return "A";
            //}
    
            //public override string AnswerB()
            //{
            //    return "B";
            //}
    
            public override string TestQuestionA()
            {
                return "A";
            }
    
            public override string TestQusetionB()
            {
                return "B";
            }
        }
    }
    View Code

    学生A的试卷类(StudentA):继承于试卷类(TestPaper)

    namespace Templete_Method_Pattern
    {
        class StudentA:TestPaper
        {
            //public override string AnswerA()
            //{
            //    return "A";
            //}
    
            //public override string AnswerB()
            //{
            //    return "B";
            //}
    
            public override string TestQuestionA()
            {
                return "A";
            }
    
            public override string TestQusetionB()
            {
                return "B";
            }
        }
    }
    View Code

    学生B的试卷类(StudentB):继承于试卷类(TestPaper)

    namespace Templete_Method_Pattern
    {
        class StudentB:TestPaper
        {
            //public override string AnswerA()
            //{
            //    return "C";
            //}
    
            //public override string AnswerB()
            //{
            //    return "D";
            //}
    
            public override string TestQuestionA()
            {
                return "C";
            }
    
            public override string TestQusetionB()
            {
                return "D";
            }
        }
    }
    View Code

    测试类(TestMain)

    namespace Templete_Method_Pattern
    {
        class TestMain
        {
            static void Main(string[] args)
            {
                Console.WriteLine("学生A的试卷:");
                TestPaper A = new StudentA();
                A.TempleteMethod();
                //A.TestQuestionA();
                //A.TestQuestionB();
    
                Console.WriteLine("学生B的试卷:");
                TestPaper B = new StudentB();
                B.TempleteMethod();
                //B.TestQuestionA();
                //B.TestQuestionB();
    
                Console.ReadLine();
            }
        }
    }
    View Code

    测试结果:

    要么忍,要么狠,要么滚!
  • 相关阅读:
    python json 访问与字符串截取
    python 12306 车次数据获取
    12306 城市代码 切片技巧
    python 9*9 乘法表
    python 列表转为字典的两个小方法
    python 三种遍历列表里面序号和值的方法
    虚拟机中访问连接在物理机上的摄像机(使用桥接)
    C++程序调用python3
    Notepad++编写运行python程序
    查看进程被哪台电脑的哪个进程连接(netstat)
  • 原文地址:https://www.cnblogs.com/zxd543/p/3246471.html
Copyright © 2011-2022 走看看