SlideShare a Scribd company logo
Decorator pattern
THE DECORATOR PATTERN
THE DECORATOR PATTERN
      CS362 Group One
THE PROBLEM
THE PROBLEM

How do you extend an existing object’s functionality
without having to create a subclass for each combination
of features?
FOR EXAMPLE
FOR EXAMPLE

Given a GUI window object, we want to add any of the
following features to the window:

  Horizontal scroll bar
  Vertical scroll bar
  Border
How can one display a window object such that it is able
to display any combination of these features without
writing a class for each individual combination?
DECORATOR PATTERN
DECORATOR PATTERN

Extends (decorates) the functionality of a certain object
at run-time

Decorating takes place independently of other instances
of the same class

The decorator class wraps around the original class or
object that is to be decorated
DECORATOR DIAGRAM
DECORATOR DIAGRAM
DECORATOR DIAGRAM
CODE—THE WINDOW


    // the Window interface
interface Window {
    public void draw(); // draws the Window
}

// implementation of a simple Window without any scrollbars
class SimpleWindow implements Window {
    public void draw() {
        // draw simple window
    }
}
CODE—THE DECORATOR


    // abstract decorator class - note that it implements Window
abstract class WindowDecorator implements Window {
    protected Window decoratedWindow; // the Window being decorated

    public WindowDecorator (Window decoratedWindow) {
        this.decoratedWindow = decoratedWindow;
    }
    public void draw() {
        decoratedWindow.draw();
    }
}
CODE—THE DECORATOR


    // the first concrete decorator which adds vertical scrollbar functionality
class VerticalScrollBarDecorator extends WindowDecorator {
    public VerticalScrollBarDecorator (Window decoratedWindow) {
        super(decoratedWindow);
    }

    public void draw() {
        decoratedWindow.draw();
        drawVerticalScrollBar();
    }

    private void drawVerticalScrollBar() {
        // draw the vertical scrollbar
    }
}
CODE—THE DECORATOR

    // the second concrete decorator which adds horizontal scrollbar
    functionality
class HorizontalScrollBarDecorator extends WindowDecorator {
    public HorizontalScrollBarDecorator (Window decoratedWindow) {
        super(decoratedWindow);
    }

    public void draw() {
        decoratedWindow.draw();
        drawHorizontalScrollBar();
    }

    private void drawHorizontalScrollBar() {
        // draw the horizontal scrollbar
    }
}
CODE—THE MAIN



    public class DecoratedWindowTest {
    public static void main(String[] args) {
        // create a decorated Window with horizontal and vertical scrollbars
        Window decoratedWindow = new HorizontalScrollBarDecorator (
                new VerticalScrollBarDecorator(new SimpleWindow()));
    }
}
QUESTIONS?

More Related Content

PDF
Grigoriu tudor 341 c5_(tudorgrigoriu@yahoo.com)_designpatterns
PPT
Patterns in PHP
PDF
Hello world e4 application part 5
PPT
15 decorator pattern
PPTX
Design Patterns - 01 Introduction and Decorator Pattern
PPT
Design patterns - Strategy Pattern
PPTX
Decorator design pattern
PPT
Design patterns - Decorator pattern
Grigoriu tudor 341 c5_(tudorgrigoriu@yahoo.com)_designpatterns
Patterns in PHP
Hello world e4 application part 5
15 decorator pattern
Design Patterns - 01 Introduction and Decorator Pattern
Design patterns - Strategy Pattern
Decorator design pattern
Design patterns - Decorator pattern

Similar to Decorator pattern (20)

PPTX
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
PDF
Bridge Pattern
PDF
VR Workshop #2
PPT
Awt and swing in java
PDF
21 -windows
PDF
UNIT-2-AJAVA.pdf
PDF
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
PPTX
U5 JAVA.pptx
PPTX
java_for_future_15-Multithreaded-Graphics.pptx
PDF
The Ring programming language version 1.5.4 book - Part 71 of 185
PPTX
OpenJFX on Android and Devices
PDF
Visual C++ project model
PPTX
Abstract vs Concrete Classes.pptx
PDF
The Ring programming language version 1.5.1 book - Part 67 of 180
PPT
(OOAD) UML Slides
PDF
Idiomatic Gradle Plugin Writing
PPT
Android classes in mumbai
PPT
Polymorphism
PPT
Polymorphism
PPT
Polymorphism
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
Bridge Pattern
VR Workshop #2
Awt and swing in java
21 -windows
UNIT-2-AJAVA.pdf
Please read this carefully needs to be in JAVA        Java 2D intr.pdf
U5 JAVA.pptx
java_for_future_15-Multithreaded-Graphics.pptx
The Ring programming language version 1.5.4 book - Part 71 of 185
OpenJFX on Android and Devices
Visual C++ project model
Abstract vs Concrete Classes.pptx
The Ring programming language version 1.5.1 book - Part 67 of 180
(OOAD) UML Slides
Idiomatic Gradle Plugin Writing
Android classes in mumbai
Polymorphism
Polymorphism
Polymorphism
Ad

Recently uploaded (20)

PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PPTX
20th Century Theater, Methods, History.pptx
PDF
IGGE1 Understanding the Self1234567891011
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
advance database management system book.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Introduction to pro and eukaryotes and differences.pptx
PPTX
Computer Architecture Input Output Memory.pptx
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
Empowerment Technology for Senior High School Guide
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
20th Century Theater, Methods, History.pptx
IGGE1 Understanding the Self1234567891011
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
What if we spent less time fighting change, and more time building what’s rig...
advance database management system book.pdf
Computing-Curriculum for Schools in Ghana
TNA_Presentation-1-Final(SAVE)) (1).pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
A powerpoint presentation on the Revised K-10 Science Shaping Paper
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
Paper A Mock Exam 9_ Attempt review.pdf.
Introduction to pro and eukaryotes and differences.pptx
Computer Architecture Input Output Memory.pptx
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Empowerment Technology for Senior High School Guide
Unit 4 Computer Architecture Multicore Processor.pptx
Share_Module_2_Power_conflict_and_negotiation.pptx
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Ad

Decorator pattern

  • 3. THE DECORATOR PATTERN CS362 Group One
  • 5. THE PROBLEM How do you extend an existing object’s functionality without having to create a subclass for each combination of features?
  • 7. FOR EXAMPLE Given a GUI window object, we want to add any of the following features to the window: Horizontal scroll bar Vertical scroll bar Border How can one display a window object such that it is able to display any combination of these features without writing a class for each individual combination?
  • 9. DECORATOR PATTERN Extends (decorates) the functionality of a certain object at run-time Decorating takes place independently of other instances of the same class The decorator class wraps around the original class or object that is to be decorated
  • 13. CODE—THE WINDOW // the Window interface interface Window { public void draw(); // draws the Window } // implementation of a simple Window without any scrollbars class SimpleWindow implements Window { public void draw() { // draw simple window } }
  • 14. CODE—THE DECORATOR // abstract decorator class - note that it implements Window abstract class WindowDecorator implements Window { protected Window decoratedWindow; // the Window being decorated public WindowDecorator (Window decoratedWindow) { this.decoratedWindow = decoratedWindow; } public void draw() { decoratedWindow.draw(); } }
  • 15. CODE—THE DECORATOR // the first concrete decorator which adds vertical scrollbar functionality class VerticalScrollBarDecorator extends WindowDecorator { public VerticalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); } public void draw() { decoratedWindow.draw(); drawVerticalScrollBar(); } private void drawVerticalScrollBar() { // draw the vertical scrollbar } }
  • 16. CODE—THE DECORATOR // the second concrete decorator which adds horizontal scrollbar functionality class HorizontalScrollBarDecorator extends WindowDecorator { public HorizontalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); } public void draw() { decoratedWindow.draw(); drawHorizontalScrollBar(); } private void drawHorizontalScrollBar() { // draw the horizontal scrollbar } }
  • 17. CODE—THE MAIN public class DecoratedWindowTest { public static void main(String[] args) { // create a decorated Window with horizontal and vertical scrollbars Window decoratedWindow = new HorizontalScrollBarDecorator ( new VerticalScrollBarDecorator(new SimpleWindow())); } }

Editor's Notes