CS 210 Introduction to Design Patterns September 14 th, 2006.

Slides:



Advertisements
Similar presentations
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Advertisements

III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
Chapter 3: The Decorator Pattern
 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
02 - Structural Design Patterns – 1 Moshe Fresko Bar-Ilan University תשס"ח 2008.
CS 210 Introduction to Design Patterns September 12 th, 2006.
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Name of.
Marcelo Santos 1 Design Patterns Object Oriented Programming Advanced Course 2007.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More design patterns.
Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
March 2004Object Oriented Design1 Object-Oriented Design.
Algorithm Programming I/O via Java Streams Bar-Ilan University תשס"ח by Moshe Fresko.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Winter 2011ACS-3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
CS 210 Introduction to Design Patterns September 28 th, 2006.
9/28/01F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Design Patterns.
Lecture 16 Composition vs Inheritance: The Final Chapter.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
CS 210 Introduction to Design Patterns September 7 th, 2006.
Session 21 Chapter 10: Mechanisms for Software Reuse.
Jan Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
Decorator Pattern So many options!. Starbuzz Coffee  Want to offer a variety of combinations of coffee and condiments  Cost of a cup depends on the.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Week 6, Class 1 & 2: Decorators Return Exam Questions about lab due tomorrow in class? Threads Locking on null object invokeLater & the squares example.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
CS 210 Review Session October 5 th, Head First Design Patterns Chapter 4 Factory Pattern.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Patterns Introduction
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
CS 210 Introduction to Design Patterns August 29, 2006.
Lecture 14 Inheritance vs Composition. Inheritance vs Interface Use inheritance when two objects share a structure or code relation Use inheritance when.
The Observer Pattern.
CS 210 Review October 3, 2006.
What if the milk price goes up? What if a new topping is added? What design principles are violated?
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:
F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator.
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.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
Week 5, Day 2: Decorator Decorators Muddiest Point Tomorrow: Quiz on lab reading: web.msoe.edu/hasker/se2811/labs/5/ SE-2811 Slide design:
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Week 5, Class 3: Decorators Lab questions? Example: Starbuzz coffee Basic Pattern More examples Design Principles Compare with alternatives SE-2811 Slide.
The Decorator Pattern Decorators in Java I/O classes SE-2811 Dr. Mark L. Hornick 1.
Builder Introduction. Intent Separate the construction of a complex object from its representation so that the same construction process can create different.
Programming with Patterns Jeremy Cronan Alliance Safety Council
Java IO Exploring the java.io package and living to talk about it.
SE 461 Software Patterns Welcome to Design Patterns.
Design Patterns: MORE Examples
Object-Orientated Analysis, Design and Programming
Chapter Nine The Strategy Pattern
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
7. Decorator, Façade Patterns
Object Oriented Design Patterns - Structural Patterns
OO Design Patterns - Decorator
Decorator Pattern Richard Gesick.
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
7. Decorator SE2811 Software Component Design
Software Design Lecture : 28.
Presentation transcript:

CS 210 Introduction to Design Patterns September 14 th, 2006

Head First Design Patterns Chapter 3 Decorator Pattern

A coffee shop example… Beverage description getDescription() Cost() DarkRoast cost() HouseBlend cost() Decaf cost() Espresso cost() What if you want to show the addition of condiments such as steamed milk, soy, mocha and whipped milk?

Page 81 Head First Design Patterns

Beverage class redone Page 83 Head First Design Patterns

Potential problems with the design so far? Solution is not easily extendable How to deal with new condiments Price changes New beverages that may have a different set of condiments – a smoothie? Double helpings of condiments

Design Principle The Open-Closed Principle Classes should be open for extension, but closed for modification.

The Decorator Pattern Take a coffee beverage object – say DarkRoast object Decorate it with Mocha Decorate it with Whip Call the cost method and rely on delegation to correctly compute the composite cost

Decorator Pattern approach Page 89 Head First Design Patterns

Computing Cost using the decorator pattern Page 90 Head First Design Patterns

Decorator Pattern The decorator pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub- classing for extending functionality.

Decorator Pattern Defined Page 91 Head First Design Patterns Decorator Pattern Defined

Decorator Pattern for Beverage Example Page 92 Head First Design Patterns

A look at Java GUI classes Java I/O uses a lot of decorator pattern

Java I/O Use of Decorator Pattern

Decorating Java I/O Classes InputStream FileInputStream StringBufferInputStream ByteArrayInputStream FilterInputStream PushBackInputStream BufferedInputStream DataInputStream LineNumberInputStream Decorators

Look at Java I/O Classes FilterInputStream BufferedInputStream

public class LowerCaseInputStream extends FilterInputStream { public LowerCaseInputStream(InputStream in) { super(in); } public int read() throws IOException { int c = super.read(); return (c == -1 ? c : Character.toLowerCase((char)c)); } public int read(byte[] b, int offset, int len) throws IOException { int result = super.read(b, offset, len); for (int i = offset; i < offset+result; i++) { b[i] = (byte)Character.toLowerCase((char)b[i]); } return result; }

public class InputTest { public static void main(String[] args) throws IOException { int c; try { InputStream in = new LowerCaseInputStream( new BufferedInputStream( new FileInputStream("test.txt"))); while((c = in.read()) >= 0) { System.out.print((char)c); } in.close(); } catch (IOException e) { e.printStackTrace(); }

Summary so far.. OO Basics Abstraction Encapsulation Inheritance Polymorphism OO Principles Encapsulate what varies Favor composition over inheritance Program to interfaces not to implementations Strive for loosely coupled designs between objects that interact Classes should be open for extension but closed for modification. OO Patterns 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. Observer Pattern defines a one-to- many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. Decorator Pattern – attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative for sub-classing for extending functionality