Presentation is loading. Please wait.

Presentation is loading. Please wait.

Template method. DCS – SWC 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.

Similar presentations


Presentation on theme: "Template method. DCS – SWC 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."— Presentation transcript:

1 Template method

2 DCS – SWC 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

3 DCS – SWC 3 How to make a pizza // From a class Pizza makePizza() { createDough(); rolloutDough(); if (mustAddTomatoSauce()) addTomatoSauce(); addToppings(); if (mustAddCheese()) addCheese(); bake(); cut(); putInBox(); }

4 DCS – SWC 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”

5 DCS – SWC 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

6 DCS – SWC 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

7 DCS – SWC 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

8 DCS – SWC 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

9 DCS – SWC 9 Method categorisation Method TypeDefinitionOverride Template Method Defines generic skeleton for algorithm No Concrete Operation Generic algorithm step No HookDefault – but overridable – algorithm step May Primitive Operation Specialised algorithm step Must

10 DCS – SWC 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

11 DCS – SWC 11 Specialised pizza public class HawaiiPizzaNoCheese extends Pizza { // Hook (overrides default behavior) public boolean mustAddCheese() { return false;} public void addToppings() // Primitive operation { addPineAppleSlices(); addShrimps(); addShreddedHam(); }

12 DCS – SWC 12 Template method pattern AbstractClass templateMethod() … primitiveOpA() primitiveOpB() primitiveOpC() Notice this is not an interface! ConcreteClass primitiveOpA() primitiveOpB() primitiveOpC() templateMethod() is never overwritten!

13 DCS – SWC 13 Template method pattern Pizza myPizza = new CopenhagenSpecialPizza();... myPizza.makePizza(); // Notice that Pizza myPizza = new Pizza() // will fail!

14 DCS – SWC 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

15 DCS – SWC 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

16 DCS – SWC 16 Template method pattern Base MakePizza Spec. AddToppings() Base Spec. MakePizza CreateDough() 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


Download ppt "Template method. DCS – SWC 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."

Similar presentations


Ads by Google