一、模板方法模式,简单的说就是在一个上层的抽象类中,定义了一些操作的抽象方法,有一个总体的方法组织了怎么去调用这个操作方法,而操作方法的具体实现由子类去实现,达到抽取公共部分放在父类模板中,子实现各自己不对的部分
public abstract class AbstractTemplate { protected virtual void Init() { Console.WriteLine("init data"); } protected abstract void Step1(); protected abstract void Step2(); protected abstract void Step3(); protected virtual void Save() { Console.WriteLine("Save Data"); } public void Run() { Init(); Step1(); Step2(); Step3(); Save(); } } public class DoA : AbstractTemplate { protected override void Init() { Console.WriteLine("DoA init data"); base.Init(); } protected override void Step1() { Console.WriteLine("DoA Step1"); } protected override void Step2() { Console.WriteLine("DoA Step2"); } protected override void Step3() { Console.WriteLine("DoA Step3"); } } public class DoB : AbstractTemplate { protected override void Step1() { Console.WriteLine("DoB Step1"); } protected override void Step2() { Console.WriteLine("DoB Step2"); } protected override void Step3() { Console.WriteLine("DoB Step3"); } protected override void Save() { Console.WriteLine("DoB Save data"); } }
标签:Console,Step1,void,模式,protected,WriteLine,override,方法,模板 From: https://www.cnblogs.com/ljx2012/p/18492132