Template method mode-the question numbers are all copied wrong

  TemplateMethod mode (TemplateMethod) definition: define the skeleton of an algorithm in operation, delay some steps to subclasses. The template method allows subclasses to redefine certain specific steps of an algorithm without changing the structure of the algorithm.

  advantage:

  • Some details of the sub-class implementation of the algorithm are helpful for the expansion of the algorithm.
  • The operation realized by calling a subclass by a parent class, and adding new behaviors by subclass extension, conforms to the "open-closed principle".
  • The code reuse is well realized, the unchanged behavior is placed in the super class, and the duplication of subclasses is removed.
    Disadvantages:
  • Each different implementation needs to define a subclass, which will lead to an increase in the number of classes and a more abstract design.

  Applicable scene:

  • In some types of algorithms, the same method is used, causing code duplication.
  • To control the extension of the subclass, the subclass must comply with the algorithm rules.

  Example background: The tutor wants to give two students the same set of questions. The two students are too stupid to distinguish the order of the questions. There is no way for the teacher to ask for another test and ask the students to write down the question (question number) first.

Examination paper category:

class TestPaper		//给出了逻辑骨架,具体实现留到了子类中
    {
    
    
        public void TestQuestion1()
        {
    
    
            Console.WriteLine("考题1的答案是[ ]a. b. c. d.");
            Console.WriteLine("答案:"+Answer1());
        }
        public void TestQuestion2()
        {
    
    
            Console.WriteLine("考题2的答案是[ ]a. b. c. d.");
            Console.WriteLine("答案:" + Answer2());
        }
        public void TestQuestion3()
        {
    
    
            Console.WriteLine("考题3的答案是[ ]a. b. c. d.");
            Console.WriteLine("答案:" + Answer3());
        }

		//逻辑的组成是一些抽象的操作,虚方法为了让子类重写自己的答案
        protected virtual string Answer1()
        {
    
       
            return "";
        }
        protected virtual string Answer2()
        {
    
    
            return "";
        }
        protected virtual string Answer3()
        {
    
    
            return "";
        }
    }

Student A’s test paper:

    class TestPaperA:TestPaper		//同学甲、乙可以有不同的实现方法
    {
    
    
        protected override string Answer1()
        {
    
    
            return "b";
        }
        protected override string Answer2()
        {
    
    
            return "c";
        }
        protected override string Answer3()
        {
    
    
            return "a";
        }
    }

Student B’s test paper:

    class TestPaperB : TestPaper
    {
    
    
        protected override string Answer1()
        {
    
    
            return "b";
        }
        protected override string Answer2()
        {
    
    
            return "c";
        }
        protected override string Answer3()
        {
    
    
            return "b";
        }
    }

Client:

        static void Main(string[] args)
        {
    
    
            Console.WriteLine("学生甲的试卷");
            //这里使用多态,将子类对象转成父类对象,方便使用父类的方法,实现了代码复用
            TestPaper studentA = new TestPaperA();
            studentA.TestQuestion1();
            studentA.TestQuestion2();
            studentA.TestQuestion3();

            Console.WriteLine("学生乙的试卷");
            TestPaper studentB = new TestPaperB();
            studentB.TestQuestion1();
            studentB.TestQuestion2();
            studentB.TestQuestion3();

            Console.ReadLine();
        }

Guess you like

Origin blog.csdn.net/CharmaineXia/article/details/110678481