1.抽象类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TemplateMethod
{
public abstract class ProjectManager
{
public void ProcessProject()
{
Console.WriteLine("抽象类 处理项目");
Review();
Design();
Coding();
Test();
}
protected abstract void Review();
protected abstract void Design();
protected abstract void Coding();
protected abstract void Test();
}
}
2.项目A
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TemplateMethod
{
public class ProjectA : ProjectManager
{
protected override void Coding()
{
Console.WriteLine("项目__代码编写");
}
protected override void Design()
{
Console.WriteLine("项目__项目设计");
}
protected override void Review()
{
Console.WriteLine("项目__项目评审");
}
protected override void Test()
{
Console.WriteLine("项目__测试");
}
}
}
3.项目B
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TemplateMethod
{
public class ProjectB : ProjectManager
{
protected override void Coding()
{
Console.WriteLine("项目__代码编写");
}
protected override void Design()
{
Console.WriteLine("项目__项目设计");
}
protected override void Review()
{
Console.WriteLine("项目__项目评审");
}
protected override void Test()
{
Console.WriteLine("项目__测试");
}
}
}
4.测试与使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TemplateMethod
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("---------------模板模式---------------");
Console.WriteLine("---项目A---------------");
ProjectManager projectManager = new ProjectA();
projectManager.ProcessProject();
Console.WriteLine("---项目B---------------");
projectManager = new ProjectB();
projectManager.ProcessProject();
Console.ReadLine();
}
}
}
结果