标签:com https 结构 定义 tor 重要 复杂 add ide
定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。 --《设计模式GoF》
using System; namespace DoFactory.GangOfFour.Template.Structural { /// <summary> /// MainApp startup class for Real-World /// Template Design Pattern. /// </summary> class MainApp { /// <summary> /// Entry point into console application. /// </summary> static void Main() { AbstractClass aA = new ConcreteClassA(); aA.TemplateMethod(); AbstractClass aB = new ConcreteClassB(); aB.TemplateMethod(); // Wait for user Console.ReadKey(); } } /// <summary> /// The ‘AbstractClass‘ abstract class /// </summary> abstract class AbstractClass { public abstract void PrimitiveOperation1(); public abstract void PrimitiveOperation2(); // The "Template method" public void TemplateMethod() { PrimitiveOperation1(); PrimitiveOperation2(); Console.WriteLine(""); } } /// <summary> /// A ‘ConcreteClass‘ class /// </summary> class ConcreteClassA : AbstractClass { public override void PrimitiveOperation1() { Console.WriteLine("ConcreteClassA.PrimitiveOperation1()"); } public override void PrimitiveOperation2() { Console.WriteLine("ConcreteClassA.PrimitiveOperation2()"); } } /// <summary> /// A ‘ConcreteClass‘ class /// </summary> class ConcreteClassB : AbstractClass { public override void PrimitiveOperation1() { Console.WriteLine("ConcreteClassB.PrimitiveOperation1()"); } public override void PrimitiveOperation2() { Console.WriteLine("ConcreteClassB.PrimitiveOperation2()"); } } }
运行结果:
设计模式系列13:模板方法模式(Template Method Pattern)
标签:com https 结构 定义 tor 重要 复杂 add ide
原文地址:https://www.cnblogs.com/mcgrady/p/10366564.html