UML: · AbstractClass () ·defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm ·定义一个抽象的原始操作来使子类实现算法步骤 ·implements a template method defining the skeleton of an algorithm The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects ·实现一个定义了算法节后的模版方法该模版方法需要原始操作和抽象类中定义的操作 · ConcreteClass () ·implements the primitive operations to carry out subclassspecific steps of the algorithm ·实现原始操作来实现子类的特殊操作 abstract class AbstractClass { public abstract void PrimitiveOperation(); public abstract void PrimitiveOperation();
public void TemplateMethod() { PrimitiveOperation(); PrimitiveOperation(); ConsoleWriteLine(); } } #region Template AbstractClass aA = new ConcreteClassA(); aATemplateMethod(); AbstractClass aB = new ConcreteClassB(); aBTemplateMethod(); #endregion 模版方法模式:定义一个操作中的算法股价而将一些步骤延迟到子类中模版方法是的子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤 模版方法模式是通过把不变行为搬移到超类取出子类中的重复代码来体现它的优势它提供了一个很好的复用平台 |