[C#] 纯文本查看 复制代码
using System;
namespace ConsoleApplication1
{
// "AbstractClass"
abstract class AbstractClass
{
// Methods
public abstract void PrimitiveOperation1();
public abstract void PrimitiveOperation2();
// The Template method
public void TemplateMethod()
{
Console.WriteLine("In AbstractClass.TemplateMethod()");
PrimitiveOperation1();
PrimitiveOperation2();
}
}
// "ConcreteClass"
class ConcreteClass : AbstractClass
{
// Methods
public override void PrimitiveOperation1()
{
Console.WriteLine("Called ConcreteClass.PrimitiveOperation1()");
}
public override void PrimitiveOperation2()
{
Console.WriteLine("Called ConcreteClass.PrimitiveOperation2()");
}
}
/// <summary>
/// Client test
/// </summary>
public class Client
{
public static void Main(string[] args)
{
// Create instance and call template method
ConcreteClass c = new ConcreteClass();
c.TemplateMethod();
}
}
}