 |
CS 635: Advanced Object-Oriented Design & Programming |
|
---|
Spring Semester, 1998
Decorator
To Lecture Notes Index
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 21-Apr-98
Contents of Doc 23, Decorator
CS 635 Doc 23 Decorator
References | slide # 1 |
| |
Decorator | slide # 2 |
...Solution 2 - Use Decorator | slide # 5 |
...Applicability | slide # 6 |
...Consequences | slide # 6 |
...Implementation Issues | slide # 7 |
...Java Example of Decorator -
Streams | slide # 8 |
References
Design Patterns: Elements of Reusable Object-Oriented
Software, Gamma, Helm, Johnson, Vlissides, Addison
Wesley, 1995, pp 175-184
Decorator
Changing the Skin of an Object
Class Structure

Runtime Structure

Motivation - Text Views
A text view has the following features:
- side scroll bar
- bottom scroll bar
- 3D border
- Flat border
This gives 12 different options:
- TextView
- TextViewWithNoBorder&SideScrollbar
- TextViewWithNoBorder&BottomScrollbar
- TextViewWithNoBorder&Bottom&SideScrollbar
- TextViewWith3DBorder
- TextViewWith3DBorder&SideScrollbar
- TextViewWith3DBorder&BottomScrollbar
- TextViewWith3DBorder&Bottom&SideScrollbar
- TextViewWithFlatBorder
- TextViewWithFlatBorder&SideScrollbar
- TextViewWithFlatBorder&BottomScrollbar
- TextViewWithFlatBorder&Bottom&SideScrollbar
How to implement?
Solution 1 - Use Object Composition

class TextView
{
Border myBorder;
ScrollBar verticalBar;
ScrollBar horizontalBar;
public void draw()
{
myBorder.draw();
verticalBar.draw();
horizontalBar.draw();
code to draw self
}
etc.
}
But TextView know about all the variations!
New type of variations require changing TextView
(and any other type of view we have)
Solution 2 - Use Decorator
Object Composition Inside out
Change the skin of an object not it guts
TextView has no borders or scrollbars!
Add borders and scrollbars on top of a TextView

Runtime Structure

Applicability
Use Decorator:
- to add responsibilities to individual objects dynamically and
transparently
- for responsibilities that can be withdrawn
- when subclassing is impractical - may lead to too many
subclasses
Consequences
More flexible than static inheritance
Avoids feature laden classes high up in hierarchy
Lots of little objects
A decorator and its components are not identical
- so checking object identification can cause problems
if ( aComponent instanceof TextView ) blah
Implementation Issues
Keep Decorators lightweight
Don't put data members in VisualComponent

Java Example of Decorator - Streams

import java.io.*;
import sdsu.io.*;
class ReadingFileExample
{
public static void main( String args[] ) throws Exception
{
FileInputStream inputFile;
BufferedInputStream bufferedFile;
ASCIIInputStream cin;
inputFile = new FileInputStream( "ReadingFileExample.java" );
bufferedFile = new BufferedInputStream( inputFile );
cin = new ASCIIInputStream( bufferedFile );
System.out.println( cin.readWord() );
for ( int k = 1 ; k < 4; k++ )
System.out.println( cin.readLine() );
}
}
© 1998, All Rights Reserved, SDSU & Roger Whitney
visitors since 21-Apr-98