Friday, August 24, 2007

Template Method : template for execution

Hi,
a new pattern today :)

scenario:
i have some classes to create pizzas (back to pizzas eh!! :) ). my cousin baked some pizzas at my place,so i do know the steps :).
so the steps are
1) use the pizza base.
2) Add toppings.
3) add sauce
4) bake


So now i have an interface IPizza:
public Interface IPizzaManufacture
{
void UsePizzaBase();
void AddToppings();
void AddSauce();
void Bake();
void createpizza ();
}

now the Pizza manufacture classes implement this interface

public Class LargeCheesePizzaManufacture :IPizzamanufacture
{
public void UsePizzaBase
{
Console.Writeline("Using a large Pizza Base");
}
public void AddSauce
{
Console.Writeline("Add Tomato sauce");
}
public void AddToppings
{
Console.Writeline("Cheese Toppings");
}

public void bake()
{
console.WriteLine("Baking now");
}
}

now if i add another class called largePapperoniPizza, i will have to implement all the methods.
nothing wrong with this.
but i notice that i will have to implement the createpizza method for every class i add, even though i dont want to introduce any new createpizza behaviour.

so 20 classes added, 20 times i implement the createpizza() method. can we avoid this?
absolutely yes:

find out the method which will remain common --now it is createpizza().

we will change the interface to an abstract Class

public Abstract Class IPizzaManufacture
{
public abstract void UsePizzaBase();
public abstract void AddToppings();
public abstract void AddSauce();
public abstract void Bake();
public virtual void CreatePizza()
{
UsePizzaBase();
AddToppings();
AddSauce();
Bake();
}

}

now i will implement this abstract class in many other classes.

public Class LargeCheesePizzaManufacture :IPizzamanufacture
{
public override void UsePizzaBase
{
Console.Writeline("Using a large Pizza Base");
}
public override void AddSauce
{
Console.Writeline("Add Tomato sauce");
}
public override void AddToppings
{
Console.Writeline("Cheese Toppings");
}
//no bake method here
//i will use the createpizza method of the abstract class
}
but notice that i will not have to implement the createpizza () method unless i want to add a custom createpizza () functionality.

public Class LargeVegetablePizzaManufacture :IPizzamanufacture
{
public override void UsePizzaBase
{
Console.Writeline("Using a large Pizza Base");
}
public override void AddSauce
{
Console.Writeline("Add Tomato and garlic sauce");
}
public override void AddToppings
{
Console.Writeline("Vegetable Toppings");
}

//i will use the a custom createpizza mechanism

public override void createpizza ()
{
Console.writeLine("createpizza for a long time");
}

}

so the createpizza () method acts as a template.

this is known as Template method because this method will create a template for execution.

happy coding !!!

1 comment:

SANDIP ROY said...

good one man.... i never knew virtual and abstract can be this powerful to force users to implement sth. Happy Pizza making. Wanna join u someday for the Pizza treat.

- Sandip