模板模式 又叫模板方法模式,在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以在不改变算法结构的情冴下,重新定义算法中的某些步骤(这个我觉得很抽象,很抽象)
案例:在一组监控的程序中,我们需要记录程序启动和结束一些信息(日志能通过上下文的信息根据约定自动生成)
//Ingore the unreasonable class name, just for presentation public abstract class BaseClass { protected void LogStart() { Console.WriteLine("Log Start Infomation"); } protected void LogEnd() { Console.WriteLine("Log End Infomation"); } public abstract void DoWork(); } public class FirstSubClass : BaseClass { public void Execute() { base.LogStart(); DoWork(); base.LogEnd(); } public override void DoWork() { Console.WriteLine(this.GetType().Name + "Do Work"); } }