template method. pba web – bewp 2 how to make a pizza if you order a pizza, the manufacturing of a...
Post on 17-Dec-2015
216 Views
Preview:
TRANSCRIPT
PBA WEB – BEWP 2
How to make a pizza
• If you order a pizza, the manufacturing of a pizza goes through certain steps
• We can write up a sort of algorithm for making a pizza
PBA WEB – BEWP 3
How to make a pizza
// From a class Pizza
MakePizza()
{
CreateDough();
RolloutDough();
if (MustAddTomatoSauce()) AddTomatoSauce();
AddToppings();
if (MustAddCheese()) AddCheese();
Bake();
Cut();
PutInBox();
}
PBA WEB – BEWP 4
How to make a pizza
• This method for creating a pizza is generic; we always follow this algorithm
• However, not all of the individual steps are generic; they vary with the actual product
• We can categorise the methods, according to their ”level of generic-ness”
PBA WEB – BEWP 5
How to make a pizza
MakePizza()
{
CreateDough();
RolloutDough();
if (MustAddTomatoSauce()) AddTomatoSauce();
AddToppings();
if (MustAddCheese()) AddCheese();
Bake();
Cut();
PutInBox();
}
Is a fully generic method for making a pizza
PBA WEB – BEWP 6
How to make a pizza
MakePizza()
{
CreateDough();
RolloutDough();
if (MustAddTomatoSauce()) AddTomatoSauce();
AddToppings();
if (MustAddCheese()) AddCheese();
Bake();
Cut();
PutInBox();
}
The red steps are fully generic – they never vary
PBA WEB – BEWP 7
How to make a pizza
MakePizza()
{
CreateDough();
RolloutDough();
if (MustAddTomatoSauce()) AddTomatoSauce();
AddToppings();
if (MustAddCheese()) AddCheese();
Bake();
Cut();
PutInBox();
}
The green steps may vary, but have a default behavior
PBA WEB – BEWP 8
How to make a pizza
MakePizza()
{
CreateDough();
RolloutDough();
if (MustAddTomatoSauce()) AddTomatoSauce();
AddToppings();
if (MustAddCheese()) AddCheese();
Bake();
Cut();
PutInBox();
}
The brown step will vary, and has no default behavior
PBA WEB – BEWP 9
Method categorisation
Method Type Definition Override
Template Method
Defines generic skeleton for algorithm
No
Concrete Operation
Generic algorithm step No
Hook Default – but overridable – algorithm step
May
Primitive Operation
Specialised algorithm step
Must
PBA WEB – BEWP 10
Specialised pizza
• How can we then make a specific pizza?
• We can let a subclass – e.g HawaiiPizza-NoCheese specialise the Pizza class
• What should this class implement..?
• Non-generic steps, i.e– Hooks– Primitive Operations
PBA WEB – BEWP 11
Specialised pizza
public class HawaiiPizzaNoCheese : Pizza
{
// Hook (overrides default behavior)
public override bool MustAddCheese() { return false;}
public override void AddToppings() // Primitive operation
{
AddPineAppleSlices();
AddShrimps();
AddShreddedHam();
}
}
PBA WEB – BEWP 12
Template method pattern
AbstractClassTemplateMethod()…PrimitiveOpA()PrimitiveOpB()PrimitiveOpC()
Notice this is not an interface!
ConcreteClassPrimitiveOpA()PrimitiveOpB()PrimitiveOpC()
TemplateMethod() is never overwritten!
PBA WEB – BEWP 13
Template method pattern
Pizza myPizza = new CopenhagenSpecialPizza();
...
myPizza.MakePizza();
// Notice that Pizza myPizza = new Pizza()
// will fail!
PBA WEB – BEWP 14
Template method pattern
• What have we achieved?– Program to an interface, not an
implementation• Clients will only know the AbstractClass interface
– Encapsulate the aspects that vary• Specialised algorithm steps are located in the
specialised classes
PBA WEB – BEWP 15
Template method pattern
• What have we achieved?– Open for extension, closed for modification
• We can add new specialisations without modification of base
– Don’t call us, we’ll call you• Base class calls specialised steps, not vice versa
• The last principle is called the Hollywood Agent principle
top related