creativity in code.

creativity is that spark of electricity before an explosion.
it allows us, in a single instance to explore all possibilities without hesitation.

Programming - Template Behavioral Design Pattern


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.


The classes and/or objects participating in this example are:


  • AbstractClass
    and
    • defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm
    • implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
  • ConcreteClass
    • implements the primitive operations ot carry out subclass-specific steps of the algorithm
using
System;

namespace DoFactory.GangOfFour.Template.Structural
{

// MainApp test application

class MainApp
{
static void Main()
{
AbstractClass c;

c = new ConcreteClassA();
c.TemplateMethod();

c = new ConcreteClassB();
c.TemplateMethod();

// Wait for user
Console.Read();
}
}

// "AbstractClass"

abstract class AbstractClass
{
public abstract void PrimitiveOperation1();
public abstract void PrimitiveOperation2();

// The "Template method"
public void TemplateMethod()
{
PrimitiveOperation1();
PrimitiveOperation2();
Console.WriteLine("");
}
}

// "ConcreteClass"

class ConcreteClassA : AbstractClass
{
public override void PrimitiveOperation1()
{
Console.WriteLine("ConcreteClassA.PrimitiveOperation1()");
}
public override void PrimitiveOperation2()
{
Console.WriteLine("ConcreteClassA.PrimitiveOperation2()");
}
}

class ConcreteClassB : AbstractClass
{
public override void PrimitiveOperation1()
{
Console.WriteLine("ConcreteClassB.PrimitiveOperation1()");
}
public override void PrimitiveOperation2()
{
Console.WriteLine("ConcreteClassB.PrimitiveOperation2()");
}
}
}

Output expected:


ConcreteClassA.PrimitiveOperation1()
ConcreteClassA.PrimitiveOperation2()


and
and

Other pages of interest:
[+] Programming - WCF
[+] Programming - Design patterns in Software
[+] Programming - Factory Method Design Pattern
[+] Scrum Development at x2codecreative ltd
[+] C++ creator, Bjarne Stroustrup bares all in Computerworld interview
[+] .NET Framework
[+] Software Testing
[+] IBM Unleashes Process Improvement
[+] Software Standards - Software houses must declare patents in standard-setting process
[+] Software Testing
[+] DAL and the nHibernate Question


Comments(0)

add comment Add Comment

Add your comment

Email address:
Your name:
Do you want your email to be visible?: