The Decorator Pattern Decorators in Java I/O classes SE-2811 Dr. Mark L. Hornick 1.

Slides:



Advertisements
Similar presentations
Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Advertisements

Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
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
SE-2811 Dr. Mark L. Hornick 1. The Decorator Pattern SE-2811 Dr. Mark L. Hornick 2.
Standard input, output and error. Lecture Under Construction.
1 Code Reuse in OOP Three categories of code reuse –Inherit an existing class SSLServerSocket extends ServerSocket –Instantiate an existing class new Thread(new.
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 ( ) ( ) ( )
Introduction to Interprocess communication SE-2811 Dr. Mark L. Hornick 1.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
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.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Week 2, Day 2: The Factory Method Pattern Other good design principles Cohesion vs. Coupling Implementing the Strategy Pattern Changing strategies (behaviors)
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
Week 6, Class 1 & 2: Decorators Return Exam Questions about lab due tomorrow in class? Threads Locking on null object invokeLater & the squares example.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
SE-2811 Software Component Design Week 1, Day 2 (and 1-3 and 2-1) SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 1.
CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann.
Copyright(c) Systems and Computer Engineering, Carleton Univeristy, * Object-Oriented Software Development Unit 13 I/O Stream Hierarchy Case.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
DEPARTMENT OF COMPUTER SCIENCE N.HARIKA. Contents Overview of I/O Streams Character Streams Byte Streams Using the Streams 2.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
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.
S.Ducasse Stéphane Ducasse 1 Decorator.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an.
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.
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 3: Decorator Decorators Muddiest Point Tomorrow: Quiz on lab reading: web.msoe.edu/hasker/se2811/labs/5/ SE-2811 Slide design:
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.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
SE2811 Week 8 – Class 2 Re: Lab due tonight SE-2811 Slide design: Dr. Mark L. Hornick Much Content: Dr. Hornick Most Errors: Dr. Yoder 1.
CS 210 Introduction to Design Patterns September 14 th, 2006.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Java IO Exploring the java.io package and living to talk about it.
java.io supports console and file I/O
CSG2H3 Object Oriented Programming
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
Exception Handling, Reading and Writing in Files, Serialization,
Slide design: Dr. Mark L. Hornick
Chapter 8: Input and Output
Week 2, Day 1: The Factory Method Pattern
I/O Basics.
7. Decorator, Façade Patterns
Chapter 17 Binary I/O Dr. Clincy - Lecture.
SE-2811 Software Component Design
lecture 08, OO Design Principle
OO Design Patterns - Decorator
Decorator Pattern Richard Gesick.
Week 7, Class 1: The Command Pattern (cont.)
Serialization and Deserialization Bullet points from Head First Java, Ch Dec-18 serialization.ppt.
08/15/09 Decorator Pattern Context: We want to enhance the behavior of a class, and there may be many (open-ended) ways of enhancing the class. The enhanced.
SE-2811 Software Component Design
7. Decorator SE2811 Software Component Design
7. Decorator, Façade Patterns
I/O and Applet from Chapter 12
Slide design: Dr. Mark L. Hornick
Week 8, Class 3: Model-View-Controller
Slide design: Dr. Mark L. Hornick
Presentation transcript:

The Decorator Pattern Decorators in Java I/O classes SE-2811 Dr. Mark L. Hornick 1

Decorator Pattern context You want to attach additional functionality to an (existing) class dynamically… …without having to resort to sub-classing the existing class We don’t want a class explosion We want to allow classes to be easily “extended” to incorporate new behavior without modifying existing code. Existing classes are closed to modification 2

SE-2811 Dr. Mark L. Hornick 3

Summary Decorators have the same super-type as the objects they decorate. One or more decorators can be used to wrap an object. Given that the decorator has the same super-type as the object it decorates, we can pass around a decorated object in place of the original (wrapped) object. The decorator adds its own behavior either before and/or after delegating to the object it decorates to do the rest of the job. Objects can be decorated at any time, so we can decorate objects at runtime with as many decorators as we like. The Decorator pattern favors Encapsulation in place of Extension aka the Open-Closed Principle

The java.io package contains dozens of classes for I/O OutputStream, FileOutputStream, PipedOutputStream, DataOutputStream, ObjectOutputStream, PrintStream, PrintWriter, … Understanding the associations between them just by reading the Javadoc API is difficult SE-2811 Dr. Mark L. Hornick 5

Knowing that the input stream classes are based on the Decorator pattern can make things easier: SE-2811 Dr. Mark L. Hornick 6

The Decorator pattern applied to input streams SE-2811 Dr. Mark L. Hornick 7

You can create custom stream decorators by extending FilterOutputStream and FilterInputStream Demonstration/Exercise SE-2811 Dr. Mark L. Hornick 8