Template method. RHS – SOC 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.

Slides:



Advertisements
Similar presentations
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Advertisements

Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Open-closed principle.
Chapter 4: The Factory Pattern. Consider the Following Code Fragment Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new.
Template method. 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.
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.
Behavioral Pattern: Template Method C h a p t e r 5 – P a g e 217 On occasion, two different components in a software system will have significant similarities,
Object Oriented Programming Lecture 7: Algorithm animation using strategy and factory patterns, The Adapter design pattern
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Design Patterns In OPM Presented by: Galia Shlezinger Instructors: Prop. Dov Dori, Dr. Iris Berger.
Command Pattern Chihung Liao Cynthia Jiang. Waiter Order Execute() Hamburger Execute() Hot Dogs Execute() Fries Execute() Cook Make Food()
Template Method By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
The Template Method By Sinclair Schuller. What is the Template Method? “Skeleton” definition of an algorithm Allows redefinition of predetermined points.
COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Setting up for TTD in Visual Studio 2012 Project | Manage NuGet Packages Select the online tab Search for Nunit Select the Nunit package Follow these instructions.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
Case Studies on Design Patterns Design Refinements Examples.
CS 350 – Software Design Template Method Pattern Let’s look at two objects public class Coffee { public void prepareRecipe() { public void prepareRecipe()
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
Command. RHS – SOC 2 Executing a command Executing a command appears simple at first, but many details to consider: –Who creates a command? –Who invokes.
Template Design Pattern Kalim Baig. Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Factory Patterns. RHS – SOC 2 Being less concrete One important OO principle is: ”Program to an interface, not an implementation” Interfaces reduces the.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
Frameworks & Patterns Use of Organized Classes. Frameworks vs Toolkits Framework Framework  Start with classes and interfaces that define a rudimentary.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
SE-2811 Software Component Design Week 1, Day 2 (and 1-3 and 2-1) SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 1.
Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Five design principles
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
CS 210 Introduction to Design Patterns August 29, 2006.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CSC 480 Software Engineering Design With Patterns.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Week 5, Day 2: Decorator Decorators Muddiest Point Tomorrow: Quiz on lab reading: web.msoe.edu/hasker/se2811/labs/5/ SE-2811 Slide design:
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Object- oriented Design Principles
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
Command. RHS – SWC 2 Executing a command Executing a command appears simple at first, but many details to consider: –Who creates a command? –Who invokes.
STRATEGY PATTERN By Michelle Johnson. BACKGROUND Behavioral Pattern Allow you to define a family of algorithms, encapsulate each one, and make them interchangeable.
Week 5, Class 3: Decorators Lab questions? Example: Starbuzz coffee Basic Pattern More examples Design Principles Compare with alternatives SE-2811 Slide.
SE 461 Software Patterns. FACTORY METHOD PATTERN.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Design Patterns: MORE Examples
Template Method Pattern Iterator Pattern
Behavioral Design Patterns
Software Design and Architecture
Programming Design Patterns
Introduction to Behavioral Patterns (3)
Object Oriented Design Patterns - Structural Patterns
Chain of Responsibility
CS 350 – Software Design Principles and Strategies – Chapter 14
Strategy and Template Method Patterns, Single User Protection
Design by Abstraction (Continuation) CS 3331 Spring 2005
CSC 480 Software Engineering
Presentation transcript:

Template method

RHS – SOC 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

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

RHS – SOC 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 ”genericity”

RHS – SOC 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

RHS – SOC 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

RHS – SOC 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

RHS – SOC 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

RHS – SOC 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

RHS – SOC 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

RHS – SOC 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(); }

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

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

RHS – SOC 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

RHS – SOC 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

RHS – SOC 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

RHS – SOC 17 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 makePizza on 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.