Template Method Pattern
Definition
Define the skeleton of an algorithm
in an operation, deferring some steps to subclasses. Template Method lets
subclasses redefine certain steps of an algorithm without changing the
algorithm's structure.
abstract class AbstractClass
{
public abstract void PrimitiveOperation1();
public abstract void PrimitiveOperation2();
public void
TemplateMethod()
{
PrimitiveOperation1();
PrimitiveOperation2();
Console.WriteLine("Call
Template Method");
}
}
//Concrete Class A
class ConcreteClassA
: AbstractClass
{
public override void PrimitiveOperation1()
{
Console.WriteLine("concreate
A primi 1");
}
public override void
PrimitiveOperation2()
{
Console.WriteLine("concreate
A primi 2");
}
}
//Concrete Class B
class ConcreteClassB
: AbstractClass
{
public override void PrimitiveOperation1()
{
Console.WriteLine("concreate
B primi 1");
}
public override void PrimitiveOperation2()
{
Console.WriteLine("concreate
B primi 2");
}
}
//calling…
//Template Method
AbstractClass a = new
ConcreteClassA();
a.TemplateMethod();
AbstractClass b = new
ConcreteClassB();
b.TemplateMethod();
0 comments:
Post a Comment