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.

Slides:



Advertisements
Similar presentations
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
Advertisements

Chapter 4: The Factory Pattern. Consider the Following Code Fragment Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new.
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,
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Design Patterns In OPM Presented by: Galia Shlezinger Instructors: Prop. Dov Dori, Dr. Iris Berger.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
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.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided.
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.
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.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
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.
 How are you going to collaborate?  How are you going to divide up work?  How are you going to make sure that changes work with other people’s code?
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.
Lexi case study (Part 2) Presentation by Matt Deckard.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
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.
Frameworks CompSci 230 S Software Construction.
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.
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.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Pattern Bridge. Definition Bridge is the structural pattern that separates abstraction from the implementation so that both of them can be changed independently.
The Factory Pattern Sanjay Yadav (ISE ).
CSC 480 Software Engineering Design With Patterns.
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.
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.
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.
Proxy. PBA WEB – BEWP 2 The Proxy pattern What is a Proxy? A Proxy is a ”placeholder” for a different object, to which we will not allow direct access.
STRATEGY PATTERN By Michelle Johnson. BACKGROUND Behavioral Pattern Allow you to define a family of algorithms, encapsulate each one, and make them interchangeable.
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
SE 461 Software Patterns. FACTORY METHOD PATTERN.
TEMPLATE METHOD DESIGN PATTERN -SWAPNIL SHAH. WHAT IS A DESIGN PATTERN… A design pattern is a general reusable solution to a commonly occurring problem.
Factory Method Pattern. Admin SCPI Patner Day Feb. 21 Lunch count Presentation (4-8 min.) Practice on Feb. 16. Morning availablity on Feb21 Brief overview.
Design Patterns: MORE Examples
Template Method Pattern Iterator Pattern
Strategy Pattern.
Behavioral Design Patterns
Software Design and Architecture
Programming Design Patterns
Introduction to Behavioral Patterns (3)
State Design Pattern 1.
Strategy and Template Method Patterns, Single User Protection
Design by Abstraction (Continuation) CS 3331 Spring 2005
“Don’t call us, we’ll call you”
CSC 480 Software Engineering
Presentation transcript:

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 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 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

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 AbstractClass TemplateMethod() … PrimitiveOpA() PrimitiveOpB() PrimitiveOpC() Notice this is not an interface! ConcreteClass PrimitiveOpA() 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

PBA WEB – BEWP 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