Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Advertisements

Lecture 15: I/O and Parsing
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
ECE452/CS446/SE464 Design Patterns: Part I Answers A Tutorial by Peter Kim Based on slides prepared by Krzysztof Pietroszek.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
02 - Structural Design Patterns – 1 Moshe Fresko Bar-Ilan University תשס"ח 2008.
COMP201 Java Programming Topic 5: Input and Output Reading: Chapter 12.
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.
Lab#1 (14/3/1431h) Introduction To java programming cs425
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
The Decorator Design Pattern (also known as the Wrapper) By Gordon Friedman Software Design and Documentation September 22, 2003.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Java I/O Input: information brought to program from an external source
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Department of Computer Science, York University Object Oriented Software Construction 16/09/ :52 PM 0 COSC3311 – Software Design Decorator Pattern.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be.
Two Ways to Store Data in a File  Text format  Binary format.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
© Amir Kirsh Files and Streams in Java Written by Amir Kirsh.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
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.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
S.Ducasse Stéphane Ducasse 1 Decorator.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Decorator Design Pattern Phillip Shin. Overview Problem Solution Example Key points.
More Patterns CS 124. More Basic Patterns Patterns you’ve already seen (without knowing it) Observer / Listener Wrapper Composite Decorator / Filter Patterns.
ECE452/CS446/SE464 Design Patterns: Part I Questions A Tutorial by Peter Kim Based on slides prepared by Krzysztof Pietroszek.
1 CSE 331 Composite Layouts; Decorators slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
java.io supports console and file I/O
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Interactive Standard Input/output
I/O Basics.
Object-Oriented Design Patterns
Decorator Intent Also known as Wrapper Example: a Text Window
Decorator Pattern Intent
Object Oriented Design Patterns - Structural Patterns
UNIT-III Structural Design Patterns
Adapter Pattern Context:
Decorator.
I/O and Applet from Chapter 12
Decorator Pattern.
Presentation transcript:

Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design

The Decorator Pattern from GoF Intent –Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub classing to extend flexibility Also Known As Wrapper Motivation –Want to add properties to an existing object. 2 Examples Add borders or scrollbars to a GUI component Add stream functionality such as reading a line of input or compressing a file before sending it over the wire

Applicability Use Decorator –To add responsibilities to individual objects dynamically without affecting other objects –When extending classes is impractical Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination (this inheritance approach is on the next few slides)

An Application Suppose there is a TextView GUI component and you want to add different kinds of borders and/or scrollbars to it You can add 3 types of borders –Plain, 3D, Fancy and 1 or 2 two scrollbars –Horizontal and Vertical An inheritance solution requires15 classes for one view next slide

That’s a lot of classes! 1.TextView_Plain 2.TextView_Fancy 3.TextView_3D 4.TextView_Horizontal 5.TextView_Vertical 6.TextView_Horizontal_Vertical 7.TextView_Plain_Horizontal 8.TextView_Plain_Vertical 9.TextView_Plain_Horizontal_Vertical 10.TextView_3D_Horizontal 11.TextView_3D_Vertical 12.TextView_3D_Horizontal_Vertical 13.TextView_Fancy_Horizontal 14.TextView_Fancy_Vertical 15.TextView_Fancy_Horizontal_Vertical

Disadvantages Inheritance solution has an explosion of classes If another view were added such as StreamedVideoView, double the number of Borders/Scrollbar classes Solution to this explosion of classes? –Use the Decorator Pattern instead

VisualComponent draw() resize() TextView draw() resize() Border draw() resize() Decorator draw() resize() ScrollBar draw() resize() SteamedVideoView draw() resize() 1 1 Plain draw() resize() 3D draw() resize() Fancy draw() resize() Decorator contains a visual component Horiz draw() resize() Vert draw() resize() An imagined example

Decorator's General Form

JScrollPane Any Component such as Container, JList, Panel can be decorated with a JScrollPane The next slide shows how to decorate a JPanel with a JScrollPane and a Border (red part)

Decorate a JPanel JScrollPane scrollPane = new JScrollPane(toStringView); add(scrollPane); // Add to a JFrame or another panel

Motivation Continued The more flexible containment approach encloses the component in another object that adds the border The enclosing object is called the decorator The decorator conforms to the interface of the component so its presence is transparent to clients The decorator forwards requests to the component and may perform additional actions before or after any forwarding

Decorator Design: Java Streams InputStreamReader(InputStream in) System.in is an InputStream object –... bridge from byte streams to character streams: It reads bytes and translates them into characters using the specified character encoding. Java TM API BufferedReader –Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. Java TM API What we had to do for console input before Java 1.5’s Scanner BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

BufferedReader readLine() // add a useful method readLine() // add a useful method InputStreamReader read() // 1 byte at a time read() // 1 byte at a time close() close() Decorator pattern in the real world BufferedReader decorates InputStreamReader Still needed to parse integers, doubles, or words

Java streams With > 60 streams in Java, you can create a wide variety of input and output streams –this provides flexibility good it also adds complexity –Flexibility made possible with inheritance and classes that accept classes that extend the parameter type

Another Decorator Example We decorated a FileInputStream with an ObjectInputStream to read objects that implement Serializable –and we used FileOutputStream with ObjectOutputStream –then we were able to use nice methods like these two read and write large complex objects on the file system: \ outFile.writeObject(list); // and later on … list = (ArrayList )inFile.readObject();

Another Decorator Example Read a plain text file and compress it using the GZIP format ZIP.java Read a compress file in the GZIP format and write it to a plain text file UNGZIP.java Sample text iliad10.txt from Project Gutenbergiliad10.txt bytes 875,736 iliad10.txt bytes 305,152 iliad10.gz 875,736 TheIliadByHomer (after code on next slide)

// Open the input file String inFilename = "iliad10.txt"; FileInputStream input = new FileInputStream(inFilename); // Open the output file String outFilename = "iliad10.gz"; GZIPOutputStream out = new GZIPOutputStream( new FileOutputStream(outFilename)); // Transfer bytes from output file to compressed file byte[] buf = new byte[1024]; int len; while ((len = input.read(buf)) > 0) { out.write(buf, 0, len); } // Close the file and stream input.close(); out.close();

// Open the gzip file String inFilename = "iliad10.gz"; GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(inFilename)); // Open the output file String outFilename = "TheIliadByHomer"; OutputStream out = new FileOutputStream(outFilename); // Transfer bytes from compressed file to output file byte[] buf = new byte[1024]; int len; while ((len = gzipInputStream.read(buf)) > 0) { out.write(buf, 0, len); } // Close the file and stream gzipInputStream.close(); out.close();

GZIPInputStream is a Decorator GZIPInputStream

Summary Decorators are very flexible alternative of inheritance Decorators enhance (or in some cases restrict) the functionality of decorated objects They work dynamically to extend class responsibilities, even inheritance does some but in a static fashion at compile time