SDSU 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

Doc 23, Decorator Slide # 2

Decorator

Changing the Skin of an Object

Class Structure
Decorator Class Structure

Runtime Structure
Decorator Runtime Structure

Doc 23, Decorator Slide # 3
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?

Doc 23, Decorator Slide # 4

Solution 1 - Use Object Composition


Text View classes using Strategy like 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)
Doc 23, Decorator Slide # 5

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

Text View using Decorator Pattern

Runtime Structure
Text View Decorator Objects


Doc 23, Decorator Slide # 6

Applicability


Use Decorator:


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


Doc 23, Decorator Slide # 7

Implementation Issues


Keep Decorators lightweight

Don't put data members in VisualComponent




Doc 23, Decorator Slide # 8

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