1 /// <summary> 2 /// 定义完整算法的各个步骤及执行顺序 3 /// </summary> 4 public abstract class AbstractClass { 5 public void TemplateMethod() { 6 PrimitiveOperation1(); 7 PrimitiveOperation2(); 8 } 9 10 protected abstract void PrimitiveOperation1(); 11 protected abstract void PrimitiveOperation2(); 12 }
AbstractClass
1 /// <summary> 2 /// 具体实现A 3 /// </summary> 4 public class ConcreteClassA:AbstractClass { 5 6 protected override void PrimitiveOperation1() { 7 Debug.Log("ConcreteClassA.PrimitiveOperation1"); 8 } 9 10 protected override void PrimitiveOperation2() { 11 Debug.Log("ConcreteClassA.PrimitiveOperation2"); 12 } 13 }
ConcreteClassA
1 /// <summary> 2 /// 具体实现B 3 /// </summary> 4 public class ConcreteClassB:AbstractClass { 5 protected override void PrimitiveOperation1() { 6 Debug.Log("ConcreteClassB.PrimitiveOperation1"); 7 } 8 9 protected override void PrimitiveOperation2() { 10 Debug.Log("ConcreteClassB.PrimitiveOperation2"); 11 } 12 }
ConcreteClassB
时间: 2024-12-14 18:44:14