What if the milk price goes up? What if a new topping is added? What design principles are violated?

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

ECE452/CS446/SE464 Design Patterns: Part I Answers A Tutorial by Peter Kim Based on slides prepared by Krzysztof Pietroszek.
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
02 - Structural Design Patterns – 1 Moshe Fresko Bar-Ilan University תשס"ח 2008.
1 Computer Science 340 Software Design & Testing Software Reuse.
CS 210 Introduction to Design Patterns September 12 th, 2006.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
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.
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,
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.
Java and C++, The Difference An introduction Unit - 00.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Java: Chapter 1 Computer Systems Computer Programming II.
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.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Lecture 16 Composition vs Inheritance: The Final Chapter.
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.
CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
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.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
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.
More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
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.
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
Lecture 14 Inheritance vs Composition. Inheritance vs Interface Use inheritance when two objects share a structure or code relation Use inheritance when.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
CS 210 Review October 3, 2006.
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.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
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.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Decorator Design Pattern Phillip Shin. Overview Problem Solution Example Key points.
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.
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.
Week 5, Class 3: Decorators Lab questions? Example: Starbuzz coffee Basic Pattern More examples Design Principles Compare with alternatives SE-2811 Slide.
Builder Introduction. Intent Separate the construction of a complex object from its representation so that the same construction process can create different.
CS 210 Introduction to Design Patterns September 14 th, 2006.
Design Patterns: Brief Examples
Inheritance and Polymorphism
Behavioral Design Patterns
I/O Basics.
7. Decorator, Façade Patterns
OO Design Patterns - Decorator
UNIT-III Structural Design Patterns
SE-2811 Software Component Design
7. Decorator SE2811 Software Component Design
Decorator.
Decorator Pattern.
Presentation transcript:

What if the milk price goes up? What if a new topping is added? What design principles are violated?

double cost(){ double total = 0; if (hasMilk()) total +=.50; if (hasSoy()) total +=.65; … … return total; } double cost(){ return super.cost() ; }

public class Beverage { public double cost(){ double total=0; if (hasMilk()) total +=.50; if (hasSoy()) total +=.65; … … return total; } public class DarkRoast extends Beverage { public DarkRoast(){ description = “Dark Roast”; } public double cost(){ return super.cost() ; }

Design Principle The Open-Closed Principle Classes should be open for extension, but closed for modification.  Using composition to extend the behaviour of objects  New functionality can be added by writing new code rather than modifying existing code  Not altering existing code reduces the chances of introducing bugs and causing unintended side effects in pre-existing code

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

Espresso $1.99

Dark Roast Coffee, Mocha, Mocha, Whip $1.49

Espresso $1.99 Dark Roast Coffee, Mocha, Mocha, Whip $1.49 House Blend Coffee, Soy, Mocha, Whip $1.34

Do we have to have this?

Decorator Pattern Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Decorators are alternatives to subclassing. Subclassing adds behaviour at compile time whereas decorators provide a new behaviour at runtime.

void doStuff(){ aComponent.doStuff(); } Type matching NOT get behaviours

aVisualComponent

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 These InputStreams act as concrete components, to be wrapped with decorators FilterInputStream is an ‘abstract’ decorator Concrete decorators Buffers input for performance Methods readLine(), mark(), reset() Ablity to count line numbers as it reads data Abstract component

Writing your own Java I/O decorator We have learned the decorator pattern And I/O class diagram Write a decorator that converts all uppercase characters to lowercase characters in the input stream

import java.io.*; 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; } Extend the FilterInputStream, the abstract decorator for all inputStream Implement 2 read() methods, taking a byte (or an array of bytes) and convert each byte to lowercase if it is an uppercase character

import java.io.*; 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(); } Set up fileInputStream and decorate it, first with BufferedInputSteam and then LowercaseInputStream filter.

Executing project IO Test.txt contains “I now know the DECORATOR PATTERN.”

Real world scenario A JScrollPane with JTextArea JTextArea area = new TextArea(10,25); JScrollPane areaScrollPane = new JScrollPane(area);

public class JBorderLabel extends JLabel { public JBorderLabel() { super(); } public JBorderLabel(String text) { super(text); } public JBorderLabel(Icon image) { super(image); } public JBorderLabel(String text, Icon image, int horizontalAlignment) { super(text, image, horizontalAlignment); } public JBorderLabel(String text, int horizontalAlignment) { super(text, horizontalAlignment); } protected void paintComponent(Graphics g) { super.paintComponent(g); int height = this.getHeight(); int width = this.getWidth(); g.drawRect(0, 0, width - 1, height - 1); } Extension by subclassing

Extension by decorator public class BorderDecorator extends JComponent { // decorated component protected JComponent child; public BorderDecorator(JComponent component) { child = component; this.setLayout(new BorderLayout()); this.add(child); } public void paint(Graphics g) { super.paint(g); int height = this.getHeight(); int width = this.getWidth(); g.drawRect(0, 0, width - 1, height - 1); }

Use Decorator Border JBorderLabel label1 = new JBorderLabel("JLabel Subclass"); BorderDecorator label2 = new BorderDecorator( new JLabel("Decorated JLabel")); BorderDecorator checkBox1 = new BorderDecorator( new JCheckBox("Decorated JCheckBox"));

Window with scrolling bar // the Window interface interface Window { public void draw(); // draws the Window public String getDescription(); // returns a description of the Window } // implementation of a simple Window without any scrollbars class SimpleWindow implements Window { public void draw() { // draw window } public String getDescription() { return "simple window"; }

// Note that it implements Window abstract class WindowDecorator implements Window { protected Window decoratedWindow; // the Window being decorated public WindowDecorator (Window decoratedWindow) { this.decoratedWindow = decoratedWindow; } Abstractor decorator class

Decorator to add vertical scrollbar // the first concrete decorator which adds vertical scrollbar functionality class VerticalScrollBarDecorator extends WindowDecorator { public VerticalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); } public void draw() { drawVerticalScrollBar(); decoratedWindow.draw(); } private void drawVerticalScrollBar() { // draw the vertical scrollbar } public String getDescription() { return decoratedWindow.getDescription() + ", including vertical scrollbars"; }

Decorator to add horizontal scrollbar // the second concrete decorator which adds horizontal scrollbar functionality class HorizontalScrollBarDecorator extends WindowDecorator { public HorizontalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); } public void draw() { drawHorizontalScrollBar(); decoratedWindow.draw(); } private void drawHorizontalScrollBar() { // draw the horizontal scrollbar } public String getDescription() { return decoratedWindow.getDescription() + ", including horizontal scrollbars"; }

Test class public class DecoratedWindowTest { public static void main(String[] args) { // create a decorated Window with // horizontal and vertical scrollbars Window simpleWindow = new SimpleWindow(); Window verticalScrollBarWindow = new verticalScrollBarDecorator(simpleWindow); Window decoratedWindow = new HorizontalScrollBarDecorator (verticalScrollBarWindow); // print the Window's description System.out.println(decoratedWindow.getDescription()); }

The TextEditor interface. package net.searchdaily.java.design.pattern.decorator; /** * Decorator Pattern Tutorial by * namnvhue * */ public interface TextEditor { public String create(); // create text file public String edit(); // edit the file public String save(); // save the change to hard disk }

package net.searchdaily.java.design.pattern.decorator; /** * Decorator Pattern Tutorial by * namnvhue * */ public abstract class TextEditorDecorator implements TextEditor { protected TextEditor enhancedTextEditor; // will support more features later public TextEditorDecorator(TextEditor textEditor) { this.enhancedTextEditor = textEditor; public String create() { return this.enhancedTextEditor.create(); public String edit() { return this.enhancedTextEditor.edit(); public String save() { return this.enhancedTextEditor.save(); } }

Notepad: The first concrete implementation package net.searchdaily.java.design.pattern.decorator; /** * Decorator Pattern Tutorial by * namnvhue * */ public class Notepad implements TextEditor { // The most basic text editor in // the public String create() { return "Notepad is Creating"; public String edit() { return "Notepad is Editing"; public String save() { return "Notepad is Saving"; } }

Notepad++ wraps a simple text editor inside and then add more features to it. package net.searchdaily.java.design.pattern.decorator; /** * Decorator Pattern Tutorial by * namnvhue * */ public class NotepadPlusPlus extends TextEditorDecorator { public NotepadPlusPlus(TextEditor textEditor) { super(textEditor); } public String edit() { // we enhance the method edit of Notepad return this.enhancedTextEditor.edit() + this.supportFormat() + this.supportFolding(); } private String supportFormat() { return " with Text Format"; } private String supportFolding() { return " and Block Folding supported"; } }

Word with even more supports package net.searchdaily.java.design.pattern.decorator; /** * Decorator Pattern Tutorial by * namnvhue * */ public class Word extends TextEditorDecorator { public Word(TextEditor textEditor) { super(textEditor); } public String edit() { StringBuilder sb = new StringBuilder(); sb.append(this.enhancedTextEditor.edit() + " with more features:n"); for (String feature : this.supportFeatures()) { sb.append(" * " + feature + "n"); } return sb.toString(); } private String[] supportFeatures() { String[] wordFeatures = { "RichText", "Image", "Table", "WordArt", "ClipArt", "And many more..." }; return wordFeatures; }

Tester class package net.searchdaily.java.design.pattern.decorator; /** * Decorator Pattern Tutorial by * namnvhue * */ public class TextEditorTester { /** args */ public static void main(String[] args) { // First we will create a very basic text editor: the Notepad Notepad basicTextEditor = new Notepad(); // See how notepad edits System.out.println(basicTextEditor.edit()); // Now we will enhance that notepad to notepad++ NotepadPlusPlus notepadPlus = new NotepadPlusPlus(basicTextEditor); // Let's see how notepad++ can edit System.out.println(notepadPlus.edit()); //Finally we will examine the Word editor Word word = new Word(basicTextEditor); System.out.println(word.edit()); } }

Adapter vs Decorator Adapter provides a different interface to its subject. Decorator provides an enhanced interface.

Problem Domain Employees are working with different responsibilities (such as team members, team leads and a manager). A team member is responsible to do his assigned tasks and to coordinate with other members for the completion of group tasks. A team lead has to manage and collaborate with his team members and plan their tasks. A manager has some extra responsibility over a team lead such as employee profiling, work assignment.

Employee: calculate salary, join, terminate. Team member: perform task, coordinate with others. Team lead: planning, motivate. Manager: assign task, employee profiling, create requirements.

Traditional Approach

Problems with traditional approach Whenever a team member becomes a team lead, or when an employee turns into a manager from a team lead/team member. Temporary fix: we have to create a new object of team lead and the previous object that points to that employee (team member) may be destroyed or archived. That’s not a recommended approach when employee is still a part of your organization. An employee can perform responsibilities of a team member as well as those of a team lead or a manager can perform team leads responsibilities. Temporary fix: You need to create two objects for the same employee which is totally wrong. In these scenarios a team member/team lead can have extra responsibilities at run time. And their responsibilities can be assigned/revoked at run time.

Decorator Pattern Approach Attach additional responsibilities to an object dynamically.

Employee (Decorated/Component) public interface Employee { public void join(Date joinDate); public void terminate(Date terminateDate); // other behaviors may reside (see sample code) }

EmployeeImpl public class EmployeeImpl implements Employee { // other behaviors and properties may reside (see sample code) public void join(Date joinDate){ print(this.getName() + ” joined on “ + joinDate); } public void terminate(Date terminateDate){ print(this.getName() + ” terminate on “ + terminateDate); } }

EmployeeDecorator (Abstract Decorator) public abstract class EmployeeDecorator implements Employee { protected Employee employee; protected EmployeeDecorator(Employee employee) { this.employee = employee; } // other behaviors may reside (see sample code) public void join(Date joinDate) { employee.join(joinDate); } public void terminate(Date terminateDate) { employee.terminate(terminateDate); } }

TeamMember (Concrete Decorator) public class TeamMember extends EmployeeDecorator { protected TeamMember(Employee employee) { super(employee); } public void performTask() { print(employee.getName() + ” is performing his assigned tasks.”); } public void coordinateWithOthers() { print(employee.getName() + ” is coordinating with other members of his team.”); } }

TeamLead (Concrete Decorator) public class TeamLead extends EmployeeDecorator { protected TeamLead(Employee employee) { super(employee); } public void planing() { print(this.employee.getName() + ” is planing.”); } public void motivate() { print(this.employee.getName() + ” is motivating his members.”); } }

Manager (Concrete Decorator) public class Manager extends EmployeeDecorator { protected Manager(Employee employee) { super(employee); } public void assignTask() { print(this.employee.getName() + ” is assigning tasks.”); } public void profileEmployee() { print(this.employee.getName() + ” is profiling employees.”); } public void createRequirments() { print(this.employee.getName() + ” is creating requirement documents.”); } }

An Evaluation Application Let’s consider a program where there are some rules that will be used in evaluating an application submitted to a University. Say the Registrar object, in our program, is responsible for carrying out the specific evaluation by using the rules. To start with say we only have been given the GPAEval as the criteria to be applied.

Registrar class

GPAEval class

Application and Test code class

Let’s Extend this now In some but not all cases, evaluate GRE scores in addition to GPA scores. How can we realize this without breaking the Registrar class?

One possibility is to derive from the GPAEval class

Test code class

Now what? Now, we are asked to evaluate the applicant based on TOEFL and GPA. How can we do that? We can write another class TOEFLEval which inherits from GPAEval and that should take care of it right?

Growing Pain Some need all GPA, GRE, TOEFL Some need two of them

Who comes to rescue? Decorator is a pattern that shows us how to solve problems like this. Another way to understand decorator is to understand chaining. The criteria objects can be chained to achieve extensibility and agility

Decorator in action I wake up on Tuesday morning to teach my favorite cosc 330 and I look in the mirror and say “you do not look good.” May take shower May wear a nice shirt and pant May wear a tie, a tie pin, etc May wear a makeup Or ear rings, nose rings, tongue rings, etc! In other words, we decorate the object with other objects.

Decorator Pattern Solution

abstract class EvaluationCriteria

Registrar class

GPAEval derived from EvaluationCriteria

CriteriaLink class

GREEval class

TOEFLEval class

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

Basing it off a different abstract class exposes this design aspect and makes it clearer how new decorations are to be added. Implementing the decorations as child classes blurs this distinction and leaves the implementation open to child classes that may break the decoration pattern unwittingly. Omitting the abstract Decorator class. There's no need to define an abstract Decorator class when you only need to add one responsibility.