熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

淺嘗DesignPattern

2022-06-13   來源: .NET編程 

  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

  模版方法模式:定義一個操作中的算法股價而將一些步驟延遲到子類中模版方法是的子類可以不改變一個算法的結構即可重定義該算法的某些特定步驟

  模版方法模式是通過把不變行為搬移到超類取出子類中的重復代碼來體現它的優勢它提供了一個很好的復用平台


From:http://tw.wingwit.com/Article/program/net/201311/13987.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.