Template Design Pattern Kalim Baig. Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

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.
Inheritance Inheritance Reserved word protected Reserved word super
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,
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Object-Oriented Thinking Chapter 1, Object-Oriented Programming in Java, Timothy Budd, 1998 ICS102 Semester
Template Method By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
Template Behavioral Pattern By Christopher Young 04/12/10 Bowring.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
COMP 121 Week 02. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided.
Inheritance using Java
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.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
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.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
Inheritance in the Java programming language J. W. Rider.
1 Computer Science 340 Software Design & Testing Inheritance.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
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.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CSC 480 Software Engineering Design With Patterns.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method 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.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
Module 9. Dealing with Generalization Course: Refactoring.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
TEMPLATE METHOD DESIGN PATTERN -SWAPNIL SHAH. WHAT IS A DESIGN PATTERN… A design pattern is a general reusable solution to a commonly occurring problem.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Design Patterns: MORE Examples
Strategy: A Behavioral Design Pattern
Template Method Pattern Iterator Pattern
Interfaces Unit 08.
Factory Patterns 1.
Interface, Subclass, and Abstract Class Review
Inheritance and Polymorphism
Behavioral Design Patterns
Software Design and Architecture
Software Design and Architecture
Strategy Design Pattern
Introduction to Behavioral Patterns (3)
Week 6 Object-Oriented Programming (2): Polymorphism
SE-2811 Software Component Design
Strategy and Template Method Patterns, Single User Protection
CS 325: Software Engineering
Design by Abstraction (Continuation) CS 3331 Spring 2005
CSC 480 Software Engineering
HFOOAD Chapter 5 Interlude
Presentation transcript:

Template Design Pattern Kalim Baig

Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How might it help the designer Significance Significance Example (Class Diagram & Code) Example (Class Diagram & Code) Hooked Template Method Hooked Template Method

What is Template? Template is actually just a method with some steps in sequence Template is actually just a method with some steps in sequence abstract ParentClass { final void TemplateMethod() { MethodStep1() MethodStep2() MethodStep3() MethodStep4() } void MethodStep1(){…} void MethodStep2(){…} …. } An abstract class defines various methods and has one non-overridden(Final) method which calls the various other methods. which calls the various other methods. subclass can modify the steps but cannot modify order, flow of control

Definition (GOF) The Template Method is know as a behavioral pattern which lets subclasses implement behaviour that can vary The Template Method is know as a behavioral pattern which lets subclasses implement behaviour that can vary Define the Skeleton of an algorithm in operation, deferring some steps to subclass Define the Skeleton of an algorithm in operation, deferring some steps to subclass Template Method lets subclasses redefine certain steps of an algorithm without changing, The algorithm’s structure. Template Method lets subclasses redefine certain steps of an algorithm without changing, The algorithm’s structure.

Problem When a majority of problem domain classes are all similar with the exception of a few classes that have deviant behavior When a majority of problem domain classes are all similar with the exception of a few classes that have deviant behavior Template method uses Inheritance to solve prolem Template method uses Inheritance to solve prolem

How might it help the designer ? Template Method" allows the designer (and the developer) to encapsulate the basic, common behavior in a base class and defer the implementation of the deviant behavior to a derived class. This has 2 advantages:- First, the code for the common behavior is not duplicated in the set of classes (obviously) and Second, the base class "looks complete" in principle. That is, looking at the base class, one can say - "Oh, OK! This class behaves in such- and-such a manner (the Template Method) and it comprises of several steps, some of which the class doesn't yet know how exactly will it implement. These steps will be defined by the derived classes subsquently" Template Method" allows the designer (and the developer) to encapsulate the basic, common behavior in a base class and defer the implementation of the deviant behavior to a derived class. This has 2 advantages:- First, the code for the common behavior is not duplicated in the set of classes (obviously) and Second, the base class "looks complete" in principle. That is, looking at the base class, one can say - "Oh, OK! This class behaves in such- and-such a manner (the Template Method) and it comprises of several steps, some of which the class doesn't yet know how exactly will it implement. These steps will be defined by the derived classes subsquently"

Significance It deals with the assignment of responsibilities between classes. The idea is to distribute responsibility between the member of a family using Inheritance It deals with the assignment of responsibilities between classes. The idea is to distribute responsibility between the member of a family using Inheritance Eliminate Duplicate code Eliminate Duplicate code let subclasses implement behaviour that can vary let subclasses implement behaviour that can vary Control at what point(s) subclassing is allowed Control at what point(s) subclassing is allowed

Basic Structure Abstract class define the template and concrete class manages the implementation

Lets build some homes BuildHouse() is different in each class Lots of Duplicate Code What is the solution? Inheritance There is no control of Method Calling Design Problem: control sequence of steps

Classes public abstract class Home { void abstract BuildHouse(); } public class SingleFamilyHome extends Home{ public void BuildHouse() { } public void createFloor(){ System.out.println("Create Floor"); } public void createWall(){ System.out.println("Create Wall"); } public void createRoof(){ System.out.println("Create Roof"); } public void createAttachedGarage(){ System.out.println("Create Garage"); } public class Condo extends Home { public void BuildHome(){ } public void createFloor(){ System.out.println("Create Floor"); } public void createWall(){ System.out.println("Create Wall"); } public void createRoof(){ System.out.println("Create Roof"); } public void createDeGarage(){ System.out.println("Create Garage"); } public class TownHome extends Home { public void BuildHome(){ } public void createFloor(){ System.out.println("Create Floor"); } public void createWall(){ System.out.println("Create Wall"); } public void createRoof(){ System.out.println("Create Roof"); } Home myNewHome= new SingleFamilyHome() myNewHome.buildHome(); myNewHome.createRoof();

Use Pull Up Method to pull the identical methods into the superclass. Pull Up MethodPull Up Method Decompose the methods so that all the extracted methods are either identical or completely different BuildHouse() Method is a Template Method

public abstract class Home { Final void BuildHouse() { createFloor(); createWall(); createRoof(); AdditionalFeature(); } public void createFloor(){ System.out.println("Create Floor"); } public void createWall(){ System.out.println("Create Wall"); } public void createRoof(){ System.out.println("Create Roof"); } abstract void AdditionalFeature(); } public class SingleFamilyHome extends Home{ public void AdditionalFeature(){ System.out.println("This is addtional feature"); } public class TownHome extends Home{ public void AdditionalFeature(){ System.out.println("This is addtional feature"); } public class Condo extends Home{ public void AdditionalFeature(){ System.out.println("This is addtional feature"); } pubic void createRoof(){ } Template Method Why final? Home myNewHome = new SingleFamilyHome(); myNewHome.buildHouse();

Hooked Template Method public abstract class Home { Final void BuildHouse() { createFloor(); createWall(); createRoof(); AdditionalFeature(); if (CustomerWantSomethingSpecial()==true) { AddSomethingSpecial () } } public void createFloor(){ System.out.println("Create Floor"); } public void createWall(){ System.out.println("Create Wall"); } public void createRoof(){ System.out.println("Create Roof"); } abstract void AdditionalFeature(); Public void AddsomethingSpecial (){ } Boolean CustomerWantSomethingSpecial() { return true; } Calling Hooked Method

Hollywood Priciple Don't call us we will call you. Hollywood Priciple allows us to prevent dependency rot. With the hollywood priciple, we allow low level components to hook themselves into a system. But high level components decide when they are needed and how... In other words high level components give 'don't call us, we will call you' treatment to low level components. The connection between Hollywood Principle and Template Method Pattern is probably somewhat apparent. In our above example Generalization is our high-level component. It has control over the sequence of methods to be called and calls on the subclasses only when they are needed for implementation of a method. Client will depend on Generalization rather than concrete SpecializationOne or SpecializationTwo which reduces dependencies in overall system. The subclasses never call abstract class directly without being 'called' first. Don't call us we will call you. Hollywood Priciple allows us to prevent dependency rot. With the hollywood priciple, we allow low level components to hook themselves into a system. But high level components decide when they are needed and how... In other words high level components give 'don't call us, we will call you' treatment to low level components. The connection between Hollywood Principle and Template Method Pattern is probably somewhat apparent. In our above example Generalization is our high-level component. It has control over the sequence of methods to be called and calls on the subclasses only when they are needed for implementation of a method. Client will depend on Generalization rather than concrete SpecializationOne or SpecializationTwo which reduces dependencies in overall system. The subclasses never call abstract class directly without being 'called' first.