Design Patterns: Template Method, Strategy, State, Bridge

Similar documents
Design Patterns: Composite, Memento, Template Method, Decorator, Chain of Responsibility, Interpreter

Template Method. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 24 11/15/2007. University of Colorado, 2007

Design Patterns: State, Bridge, Visitor

Design Patterns. State. Oliver Haase

MORE DESIGN PATTERNS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 24 11/10/2011

Depth-First Search and Template Patterns

Tecniche di Progettazione: Design Patterns

Template Method, Iterator & Composite

Design Patterns Reid Holmes

Design to interfaces. Favor composition over inheritance Find what varies and encapsulate it

Template Method Pa.ern and Iterator Pa.ern

Several patterns can be implemented elegantly using closures.

Design Patterns: Prototype, State, Composite, Memento

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Implementing GUI context-sensitive help... ECE450 Software Engineering II. Implementing GUI context-sensitive help... Context-sensitive help

Writing your own Java I/O Decorator p. 102 Tools for your Design Toolbox p. 105 Exercise Solutions p. 106 The Factory Pattern Baking with OO

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Parallel access to linked data structures

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

[ 5.4] What cache line size is performs best? Which protocol is best to use?

CS111: PROGRAMMING LANGUAGE II

Tecniche di Progettazione: Design Patterns

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

CSE 70 Final Exam Fall 2009

a correct statement? You need to know what the statement is supposed to do.

Inheritance and Polymorphism in Java

CSC207H: Software Design Lecture 6

CSCU9T4: Managing Information

EINDHOVEN UNIVERSITY OF TECHNOLOGY

Design patterns. Jef De Smedt Beta VZW

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute.

Design Pattern and Software Architecture: IV. Design Pattern

Object-Oriented Design

Software Development 2

Pattern-Oriented Software Design (Fall 2012) Homework #2 (Due: 10/19/2012)

Design Patterns. Produced by. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Three parallel-programming models

Software Design COSC 4353/6353 D R. R A J S I N G H

Modelling with Classes. CITS1220 Software Engineering

OBJECT ORIENTED PROGRAMMING. Abstract Class And Interface

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

John W. Jacobs Technology Center 450 Exton Square Parkway Exton, PA Introduction to

Object-Oriented Design

TDDB84. Summary & wrap-up & some tips & some design patterns & some other stuff. tisdag 20 oktober 15

The Design Patterns Matrix From Analysis to Implementation

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Observer Templates. CS 5010 Program Design Paradigms Lesson 1.4

Software Design and Analysis for Engineers

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

Expanding Our Horizons. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 9 09/25/2011

John W. Jacobs Technology Center 450 Exton Square Parkway Exton, PA Introduction to

MSO Lecture 6. Wouter Swierstra (adapted by HP) September 28, 2017

MSO Exam November 7, 2016, 13:30 15:30, Educ-Gamma

Design Patterns Reid Holmes

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Topics in Object-Oriented Design Patterns

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

MSO Lecture 12. Wouter Swierstra (adapted by HP) October 26, 2017

Database Systems. Sven Helmer. Database Systems p. 1/567

Outline. Purpose. Useful Definitions. Pattern Overview

CSC/ECE 517: Object-Oriented Languages and Systems Summer 2008 Test 2 with Answers

SUMMARY: MODEL DRIVEN SECURITY

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

Bridge Pattern. Used to decouple an abstraction from its implementation so that the two can vary independently

5 R1 The one green in the same place so either of these could be green.

2.1 Introduction UML Preliminaries Class diagrams Modelling delegation... 4

Instructor: Padraic Bartlett. Lecture 2: Schreier Diagrams

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Inheritance and Interfaces

Object-Oriented Design

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Week 8: The fundamentals of graph theory; Planar Graphs 25 and 27 October, 2017

OO Frameworks. Introduction. Using Frameworks

Modules and Mixins. module Trig PI = def Trig.sin(x) #.. end CSC 517

CPSC 427a: Object-Oriented Programming

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

MSO Lecture 1. Wouter Swierstra (adapted by HP) September 11, 2017

Mutating Object State and Implementing Equality

MSO Lecture 6. Wouter Swierstra. September 24, 2015

The Software Design Process. CSCE 315 Programming Studio, Fall 2017 Tanzir Ahmed

Object-Oriented Software Engineering Practical Software Development using UML and Java

Information Systems Analysis and Design. XIV. Design Patterns. Design Patterns 1

Introduction to Object-Oriented Programming

Introduction to Design Patterns

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 3

[ ] In earlier lectures, we have seen that switches in an interconnection network connect inputs to outputs, usually with some kind buffering.

Object-Oriented Oriented Programming

Why Use Graphs? Test Grade. Time Sleeping (Hrs) Time Sleeping (Hrs) Test Grade

Introducing the UML Eng. Mohammed T. Abo Alroos

3 Continuous Integration 3. Automated system finding bugs is better than people

An Introduction to Object Orientation

Cheng, CSE870. More Frameworks. Overview. Recap on OOP. Acknowledgements:

CS342: Software Design. November 21, 2017

CS11 Introduction to C++ Fall Lecture 7

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Relaxed Memory-Consistency Models

The Template Method Pattern

Transcription:

Design Patterns: Template Method, Strategy, State, Bridge [HFDP, Ch. 8] Say we have two classes whose code looks similar, but isn t identical. We can duplicate the code, but change it where changes are needed. Is this a good idea? Outline for Lecture 25 I. The Template Method pattern II. Template Method vs. Strategy III. State vs. Strategy IV. Bridge Template Method pattern is a way of abstracting similar behavior in various classes and duplicating only the code that differs. Here s the example given in the text (p. 282): Coffee void preparerecipe() { boilwater(); brewcoffeegrinds(); pourincup(); addsugarandmilk(); Tea void preparerecipe() { boilwater(); steepteabag(); pourincup(); addlemon(); Do you see the similarity? How might you avoid code duplication? Try to think of more than one way, and submit your answer here. How should we write the code? Fill in the blanks here. public { preparerecipe() { boilwater(); ; pourincup(); ; Lecture 25 Object-Oriented Languages and Systems 1

What other declarations do we need in this class? void boilwater() { System.out.println("Boiling water "); void pourincup() { System.out.println("Pouring "); ; ; What methods do the Coffee and Tea classes need to implement? Now, let s draw the class diagram, like it says in the book This is the Template Method pattern. Which method in it is a template method? Now, let s write up the pattern definition like we have done for other patterns before. Template Method Intent: Capture common parts of an algorithm in a single code sequence. Problem: Solution: CSC/ECE 517 Lecture Notes 2009 Edward F. Gehringer 2

Implementation: Can you think of other examples of Template Method? One of the main pitfalls in Template Method is that sometimes processes are almost the same, but not exactly. Certain steps need to be performed in some cases, but not in others. For example, in the coffee case, a customer may not want sugar and milk. What s the best way to address this? Our code now becomes public abstract class CaffeineBeverage { final void preparerecipe() { boilwater(); brew(); pourincup(); The hook method is given a default implementation, which can be overridden by subclasses. Lecture 25 Object-Oriented Languages and Systems 3

Template Method helps avoid the pitfall of dependency rot. This means that high-level components (methods of the abstract class) depend on low-level components, as well as vice versa. In this context, what does it mean to depend on? Why would this be bad? The dependency-inversion principle says, Depend on abstractions. Do not depend on concrete classes. What example of the dependency-inversion principle did we see in the last lecture? To prevent dependency inversion, we apply the Hollywood principle: How can this be realized in the Template Method pattern? How can we prevent the subclass methods from calling methods of the abstract class? One commentator goes even further. He says that multiple layers of Template Methods are inherently prone to the yo-yo effect. What do you think? Template Method vs. Strategy Recall the Strategy pattern from Lecture 13. In this case, you need to choose an algorithm for a task depending on some parameter of the situation. CSC/ECE 517 Lecture Notes 2009 Edward F. Gehringer 4

For example, consider quadrature (numerical integration) again. Each time you calculate the area of a region, you need to know what the function is that you are calculating the region underneath. The definition of Strategy from HFDP is, The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Let s provide a definition of Strategy in the format that we have used before. Strategy Intent: To decouple algorithms from the objects that use them. Problem: Solution: Implementation: What s different about the two patterns? Lecture 25 Object-Oriented Languages and Systems 5

Strategy vs. State [HFDP, 10.10] Hmmm that s interesting. Strategy lets objects appear to change class. Isn t that what State does? How can we tell the difference between Strategy and State? Bridge [HFDP, 14.1] Now, let s look at the class diagram for the State pattern (for example, at http://www.exciton.cs.rice.edu/javaresources/designpatterns/statepat.htm). CSC/ECE 517 Lecture Notes 2009 Edward F. Gehringer 6

Note that the Context object holds a reference to an abstract class. This is the abstract class from which the concrete states are derived. It turns out that the same structure is valuable for implementing drivers. What s a driver? Drivers are generally used by various applications. Consider a printer driver, for example. There is a driver for a particular printer for each OS. Many different applications can use that printer, providing that the right driver is present. This is an example of the bridge pattern. We have various abstractions (the applications) and various implementations (the drivers). If the applications aren t in a hierarchy, the class diagram for Bridge is exactly the same as the class diagram for State. But often, the abstractions are in a hierarchy. HFDP gives the example of televisions and remotes. TVs have behavior in common, and so do remotes. If they re universal remotes, they can be used by lots of different kinds of TVs. This leads to the UML diagram on p. 613 of HFDP, or view it online. What are the advantages of Bridge? OK, we just told you the right way to handle this problem. What would be the wrong way? Can you imagine using a lot more subclasses? So one way of looking at Bridge is that it avoids unnecessary subclassing. Let s describe the pattern. Bridge Intent: To decouple interfaces from the implementations that use them. Lecture 25 Object-Oriented Languages and Systems 7

Problem: Solution: Implementation: Can you think of other examples of Bridge? CSC/ECE 517 Lecture Notes 2009 Edward F. Gehringer 8