c# 模板方法
abstract class AbstractClass { public void TemplateMethod() { PrimitiveOperation1(); PrimitiveOperation2(); } protected abstract void PrimitiveOperation1(); protected abstract void PrimitiveOperation2(); } public class MB{ public static void Show(){ AbstractClass abstractClass = new ConcreteClassA(); abstractClass.TemplateMethod(); Console.WriteLine(); abstractClass = new ConcreteClassB(); abstractClass.TemplateMethod(); } } class ConcreteClassA : AbstractClass { protected override void PrimitiveOperation1() { Console.WriteLine("ConcreteClassA.PrimitiveOperation1() called"); } protected override void PrimitiveOperation2() { Console.WriteLine("ConcreteClassA.PrimitiveOperation2() called"); } } class ConcreteClassB : AbstractClass { protected override void PrimitiveOperation1() { Console.WriteLine("ConcreteClassB.PrimitiveOperation1() called"); } protected override void PrimitiveOperation2() { Console.WriteLine("ConcreteClassB.PrimitiveOperation2() called"); } }