180 likes | 508 Views
Template method. 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. How to make a pizza. // From a class Pizza makePizza() { createDough(); rolloutDough();
E N D
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 RHS – SOC
How to make a pizza // From a class Pizza makePizza() { createDough(); rolloutDough(); if (mustAddTomatoSauce()) addTomatoSauce(); addToppings(); if (mustAddCheese()) addCheese(); bake(); cut(); putInBox(); } RHS – SOC
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 ”genericity” RHS – SOC
How to make a pizza Is a fully generic method for making a pizza makePizza() { createDough(); rolloutDough(); if (mustAddTomatoSauce()) addTomatoSauce(); addToppings(); if (mustAddCheese()) addCheese(); bake(); cut(); putInBox(); } RHS – SOC
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 RHS – SOC
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 RHS – SOC
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 RHS – SOC
Method categorisation RHS – SOC
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 RHS – SOC
Specialised pizza public class HawaiiPizzaNoCheese extends Pizza { // Hook (overrides default behavior) publicboolean mustAddCheese() { returnfalse;} publicvoid addToppings() // Primitive operation { addPineAppleSlices(); addShrimps(); addShreddedHam(); } } RHS – SOC
Template method pattern AbstractClass templateMethod() … primitiveOpA() primitiveOpB() primitiveOpC() Notice this is not an interface! ConcreteClass primitiveOpA() primitiveOpB() primitiveOpC() templateMethod() is never overwritten! RHS – SOC
Template method pattern Pizza myPizza = new CopenhagenSpecialPizza(); ... myPizza.makePizza(); // Notice that Pizza myPizza = new Pizza() // will fail! RHS – SOC
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 RHS – SOC
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 RHS – SOC
Base Base MakePizza CreateDough() AddToppings() Spec. MakePizza Spec. Template method pattern Don’t call us, we’ll call you - High-level classes are in control - Low-level classes can be hooked onto a system - Low-level classes don’t call high-level classes directly RHS – SOC
Exercises • Download the NetBeans project TemplateMethodExample from the Website (go to Classes, Week 43) • Examine the code; the class Pizza implements a template method makePizza for making a pizza. The template method contains concrete operations, hooks and primitive operations • Recall that hooks may – and primitive operations must – be overrided in specialised classes. Two such classes have been added; HawaiiPizza and VegetarianPizza. Examine their implementation. • A test of making concrete pizzas is found in Main. Try it out! Notice how you just call makePizzaon the specialised class objects, in order to make a specific pizza • Try adding a couple of additional specialised pizza classes to the project, and add tests of them to the test. RHS – SOC