Introduction to Behavioral Patterns (3)

Slides:



Advertisements
Similar presentations
Computer Science 313 – Advanced Programming Topics.
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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
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,
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
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.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Informatics 122 Software Design II Lecture 6 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
+ Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
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
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
CS 350 – Software Design Template Method Pattern Let’s look at two objects public class Coffee { public void prepareRecipe() { public void prepareRecipe()
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
1 KC Web & Java – 29 november 2005 – Design Patterns – The Template Method AJAX! KC Web & Java 29 november 2005.
Template Design Pattern Kalim Baig. Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How.
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.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Template Methods Ordering What We Do. Example - Solitaire Initialization of many solitaire games follow this pattern: Shuffle the cards Layout the game.
Programmeerimine Delphi keskkonnas MTAT Programmeerimine Delphi keskkonnas MTAT Jelena Zaitseva
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
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.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Classes, Interfaces and Packages
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.
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Computer Science 313 – Advanced Programming Topics.
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.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
TEMPLATE METHOD DESIGN PATTERN -SWAPNIL SHAH. WHAT IS A DESIGN PATTERN… A design pattern is a general reusable solution to a commonly occurring problem.
Design Patterns: MORE Examples
Sections Inheritance and Abstract Classes
Template Method Pattern Iterator Pattern
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Behavioral Design Patterns
Software Design and Architecture
Phil Tayco Slide version 1.1 Created Oct 30, 2017
object oriented Principles of software design
Design Patterns
Object-Oriented Programming
CSC 205 Java Programming II
Object Oriented Design Patterns - Behavioral Patterns
Object-Oriented Programming
Overview of C++ Polymorphism
CS 325: Software Engineering
Design by Abstraction (Continuation) CS 3331 Spring 2005
CSC 480 Software Engineering
Chapter 7 Inheritance.
Presentation transcript:

Introduction to Behavioral Patterns (3) 11/17/2018 Programming Design Patterns

Programming Design Patterns Making Coffee and Tea public class Coffee { void prepareRecipe() { boilWater (); brewCoffeeGrinds(); pourInCup(); addSugarAndMilk(); } // … public class Tea { void prepareRecipe() { boilWater (); steepTeaBag(); pourInCup(); addLemon(); } // … Programming Design Patterns 11/17/2018

Abstract the commonality (1) Is there code duplication in the two classes? Which two methods are the same? Need to abstract the commonality into a base class CLASSWORK Define a base class CaffeineBeverage force subclasses to implement the prepare() method let subclasses reuse the base class implementation for boilWater() and pourInCup() Programming Design Patterns 11/17/2018

Abstract the commonality (2) CaffeineBeverage: prepareRecipe() abstract prepare() boilWater() // shared pourInCup() // shared Coffee prepare() brewCoffee() addSugar() Tea preprare() steepTeaBags() addLemon() Programming Design Patterns 11/17/2018

Abstract the commonality (3) Coffee brewCoffee() addSugar() Tea steepTeaBags() addLemon() Beverage (after further abstraction) brew addCondiments Now design for the class for CaffeineBeverage Programming Design Patterns 11/17/2018

Programming Design Patterns Template Method Defines the steps of an algorithm and allows subclasses to provide the implementation for one or more steps Programming Design Patterns 11/17/2018

Programming Design Patterns CaffeineBeverage Should the class be abstract? Should the method prepare() be final? Should some of the methods of the class be abstract? Which pattern does this remind you of? Homework: difference between this pattern and “factory method” pattern Hint: one is a specialized case of the other This is similar to the factory method pattern Programming Design Patterns 11/17/2018

CaffeineBeverage class public abstract class CaffeineBeverage { public final void prepareRecipe() { boilWater(); brew(); pourCup(); addCondiments(); } protected abstract void brew(); protected abstract void addCondiments(); Programming Design Patterns 11/17/2018

Programming Design Patterns Classwork Write code for a “Tea” class with placeholder implementation (…) for the two methods brew addCondiments Write driver code to call prepareRecipe() on a “Tea” instance Programming Design Patterns 11/17/2018

Programming Design Patterns Tea class public class Tea extends CaffeineBeverage { public void brew() { …} public void addCondiments() { …} } Programming Design Patterns 11/17/2018

Template Method Pattern: usage Convert similar operations to a template. Convert from many specialized operations to a generalized operation. Refactor common behavior to simplify the code. Programming Design Patterns 11/17/2018

Template Method Pattern: definition Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. Programming Design Patterns 11/17/2018

Template Method Pattern What if a customer wants tea or coffee without condiments? The pattern provides a hook Programming Design Patterns 11/17/2018

Template Method Pattern: hook public abstract class TemplateClass { public final void templateMethod() { primitiveOperation1(); primitiveOperation2(); if (hook()) { concreteOperation(); } protected abstract void primitiveOperation1(); protected abstract void primitiveOperation2(); final void concreteOperation(); protected boolean hook() { …} Programming Design Patterns 11/17/2018

Template Method Pattern: hook (2) public abstract class Beverage { public final void templateMethod() { primitiveOperation1(); primitiveOperation2(); if (customerWantsCondiments()) addCondiments(); } protected abstract void addCondiments(); // subclasses can override this hook, if they want protected boolean customerWantsCondiments() { return true; Programming Design Patterns 11/17/2018

Programming Design Patterns Hollywood Principle "Hollywood principle" "Don't call us, we'll call you” Clients of Tea/Coffee use the superclass abstraction. reduces dependency between driver/client code and the subclasses Homework How is the “Hollywood Principle” different from “Dependency Inversion Principle” Note that the parent calls the operations of the subclass, which is less common than child code referring to the parent's code Programming Design Patterns 11/17/2018

Programming Design Patterns Design Problem A mortgage loan application consists of: check bank balance, check credit score, check stocks, check client’s potential future earnings An equity loan consists of: check values of assets, check credit score, check stocks, check increments in salary in the next 5 years. If the client is a bank employee then stocks need not be checked Present your design for this problem Programming Design Patterns 11/17/2018

Template Method Pattern (1) You can vary behavior using a simple kind of inheritance The idea is that most of an algorithm or procedure is fixed; the detailed behavior depends on calls to specific operations at certain locations Template methods are a fundamental technique for code reuse They are particularly important in class libraries, although its use is often hidden in the implementation and may not be seen in the API Programming Design Patterns 11/17/2018