把子类中重复的行为移到父类的模板方法中去。
using System.IO;
using System;
namespace 模版方法模式
{
class Program
{
static void Main(string[] args)
{
AbstractClass ac;
ac = new ConcreteClassA();
ac.TemplateMethod();
ac = new ConcreteClassB();
ac.TemplateMethod();
Console.Read();
}
abstract class AbstractClass
{
public abstract void AbstractOperation1();
public abstract void AbstractOperation2();
public void TemplateMethod()
{
Console.WriteLine("模版方法模式");
AbstractOperation1();
AbstractOperation2();
}
}
class ConcreteClassA : AbstractClass
{
public override void AbstractOperation1()
{
Console.WriteLine("具体类A方法1实现");
}
public override void AbstractOperation2()
{
Console.WriteLine("具体类A方法2实现");
}
}
class ConcreteClassB : AbstractClass
{
public override void AbstractOperation1()
{
Console.WriteLine("具体类B方法1实现");
}
public override void AbstractOperation2()
{
Console.WriteLine("具体类B方法2实现");
}
}
}
}