zoukankan      html  css  js  c++  java
  • 大话设计-模板方法模式

    在基类定义算法的结构,具体实现延迟到子类。

    using System;
    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
                TestPaper testPaperA = new TestPaperA();
                testPaperA.TestQuestion1();
                testPaperA.TestQuestion2();
                testPaperA.TestQuestion3();
                TestPaper testPaperB = new TestPaperB();
                testPaperB.TestQuestion1();
                testPaperB.TestQuestion2();
                testPaperB.TestQuestion3();
            }
        }
    
        class TestPaper
        {
            // 把骨架定义下来,只有答案不同,让子类实现具体答案。
            public void TestQuestion1()
            {
                Console.WriteLine("题目1:XXXXXXYYYYXXXMMM");
                Console.WriteLine($"题目1答案:{Answer1()}");
            }
            protected virtual string Answer1() { return ""; }    
                    
            public void TestQuestion2()
            {
                Console.WriteLine("题目2:IIIPPPKKKKK");
                Console.WriteLine($"题目2答案:{Answer2()}");
            }
            protected virtual string Answer2() { return ""; }
    
            public void TestQuestion3()
            {
                Console.WriteLine("题目3:UUUUKKKKOOO");
                Console.WriteLine($"题目3答案:{Answer3()}");
            }
            protected virtual string Answer3() { return ""; }
        }
    
        class TestPaperA : TestPaper
        {
            protected override string Answer1()
            {
                return "a";
            }
            protected override string Answer2()
            {
                return "b";
            }
            protected override string Answer3()
            {
                return "c";            
            }
        }
        class TestPaperB : TestPaper
        {
            protected override string Answer1()
            {
                return "b";
            }
            protected override string Answer2()
            {
                return "c";
            }
            protected override string Answer3()
            {
                return "a";
            }
        }
    }
    View Code
    using System;
    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
                ConcreteClass concreteClass = new ConcreteClass();
                concreteClass.TemplateMethod();
            }
        }  
    
       abstract class AbstractClass
        {
            public abstract void PrimitiveOperation1();
            public abstract void PrimitiveOperation2();
            public void TemplateMethod()
            {
                PrimitiveOperation1();
                PrimitiveOperation2();
                Console.WriteLine("");
            }
        }
    
        class ConcreteClass : AbstractClass
        {
            public override void PrimitiveOperation1()
            {
                Console.WriteLine("具体操作1");
            }
    
            public override void PrimitiveOperation2()
            {
                Console.WriteLine("具体操作2");
            }
        }
    }
  • 相关阅读:
    zoj 3279 线段树 OR 树状数组
    fzu 1962 树状数组 OR 线段树
    hdu 5057 块状链表
    hdu3487 Play with Chain
    bzoj 1588营业额统计(HNOI 2002)
    poj2823 Sliding Window
    poj2828 Buy Tickets
    poj2395 Out of Hay
    poj3667 Hotel
    poj1703 Lost Cows
  • 原文地址:https://www.cnblogs.com/bibi-feiniaoyuan/p/template.html
Copyright © 2011-2022 走看看