大话设计模式 - 模板方法(Template Method)模式

  • 模式定义算法

定义一个操做中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类能够不改变一个算法的结构便可重定义该算法的某些步骤。c#

  • 模式结构ide

wKioL1PWYCWQpeDaAAAWhQ1APAw952.png

  • 模板方法模式调用顺序图ui

    wKioL1PWYHGhLEMkAAApYJstH5o227.png

  • 模板方法缺点orm

    模板和子类是紧耦合,若是对模板中的算法骨架进行升级,可能会要求对全部相关子类的进行相应的变化。blog

  • 具体例子get

    FileGenerator(Hoovers)string




     

public void Generate()
        {
            FillShares();
            FillWithAdditionalShares();
            Log.Info("","",string.Format("Shares count:{0}", shares.Count));
            InitBlocks();
            Log.Info("","","InitBlocks work done");
            FillBlocks();
            Log.Info("","","FillBlocks work done");
            GenerateFile();
            Log.Info("","","Processed " + shares.Count + " shares.");
        }


      

protected abstract DataBlock InitBlockType(string shareClassId);

APIBaseDataCategoryDump(EquityAPI)it

相关文章
相关标签/搜索