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

Slides:



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

Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
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.
Java I/O The Cheat Sheet. Reading and Writing The basic architecture of Java IO is pluggable. The idea is to have some very simple classes do very simple.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
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.
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
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
Algorithm Programming I/O via Java Streams Bar-Ilan University תשס"ח by Moshe Fresko.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
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.
HANGMAN Project by Lori Kraus 12/3/03. WWWWe’ve played the game, now let’s see what goes on behind the scenes…. O \ | / | d b …but first, a few new.
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.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Session 21 Chapter 10: Mechanisms for Software Reuse.
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.
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.
GoF: Document Editor Example Rebecca Miller-Webster.
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.
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.
1 Software 1 Java I/O. 2 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating.
Streams & Files. Java I/O Classes Saving data to / Loading data from files Two Choices: Binary-Formatted or Text-Formatted Data – int x = 1234; – Binary.
– 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.
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.
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.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
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.
F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator.
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.
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.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
java.io supports console and file I/O
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
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
UNIT-III Structural Design Patterns
Adapter Pattern Context:
Decorator.
I/O and Applet from Chapter 12
Decorator Pattern.
Decorator Pattern The decorator pattern allows us to enclose an object inside another object. The enclosing object is called a decorator. The other object.
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 subclassing to extend flexibility Also Known As Wrapper Motivation –Want to add properties to an existing object. 3 Examples Add borders or scrollbars to a GUI component Add headers and footers to an advertisement 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

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 With another type of border added, many more classes would be needed with this design? If another view were added such as StreamedVideoView, double the number of Borders/Scrollbar 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 (not real) example

Java Borders Any JComponent can have 1 or more borders Borders are useful objects that, while not themselves components, know how to draw the edges of Swing components Borders are useful not only for drawing lines and fancy edges, but also for providing titles and empty space around components For more on Borders, The Java Tutorial

Java Code: Add a Beveled Border toStringView.setBorder(new BevelBorder( BevelBorder.LOWERED, Color.BLUE, Color.RED));

Decorate Again JScrollPane scrollPane = new JScrollPane(toStringView); add(scrollPane, BorderLayout.CENTER);

Motivation Continued The more 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 Pattern in Java 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 has to do for console input up to Java 1.4 before Scanner BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

BufferedReader readLine() readLine() InputStreamReader read() close() read() close() Example of decorator pattern use BufferedReader decorates InputStreamReader

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 bad –Flexibility made possible with inheritance and classes that accept classes that extend the parameter type You can have an InputStream instance or any instance of a class that extends InputStream public InputStreamReader(InputStream in)

One Constructor for many subclasses InputStream has these direct known subclasses: ByteArrayInputStream, FileInputStream, FilterInputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream System.in is an instance of InputStream

Another Decorator Example We also decorated a FileInputStream with an ObjectInputStream so you can read objects that implement Serializable

Another Example as a Code Demo 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 the output file to the 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 the compressed file to the output file byte[] buf = new byte[1024]; int len; while ((len = gzipInputStream.read(buf)) > 0) { out.write(buf, 0, len); for (int i = 0; i < len; i++) System.out.print((char) buf[i]); System.out.println(); } // Close the file and stream gzipInputStream.close(); out.close();