File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Decorator Design Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Decorator Design Pattern" Watch "Decorator Design Pattern" New topic

Decorator Design Pattern

Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
This is what I read about decorator design pattern.

Decorators prove more flexible than inheritance because the relationship between decorators and the objects they decorate can change at runtime, but relationships between base classes and their extensions are fixed at compile time.

Can someone please help me understand what it means by "relationship between decorators and the objects they decorate can change at runtime".

Thanks for your help in advance.

Adrabi Abderrahim

Joined: Jan 23, 2011
Posts: 8


I tell you what I've understand from decorator pattern ,

decorator is like you buy a Playstation or any other thing, you can custom style by yourself (change at runtime), but you can't change into Playstation "base class", euh, in other word classes that decorate your object can be changed at runtime, example:

you've one class base 'X', and two classes for decoration 'Y', 'Z', and you've many possibilities to decorate 'X',

Z and Y is changeable in runtime, but X no,
Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

Welcome to the Ranch, Adrabi.

OCUP UML fundamental and ITIL foundation
Adrabi Abderrahim

Joined: Jan 23, 2011
Posts: 8

thank you, dear Jan Cumps
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1012
here you can fine most of design patterns with good examples:
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

You can see the Decorator pattern in action in the little icon beside a file or folder name in a GUI such as Windows Explorer or Finder in OS X. If you have installed a client extension for Subversion, for example, you will see the icon for files and folders "decorated" with some additional icon that indicates the state of that file or folder with regard to Subversion (new, modified, in conflict, not changed, etc.) This "decoration" can change at runtime as you perform actions on your files and folders. On my Mac, when I first create a file, its icon will be decorated with a little question mark to indicate that Subversion hasn't been told what to do with it yet. When I add the file or folder to Subversion, its icon will be decorated with a "+" sign to indicate that it has been added but not committed. And so on. That is to say, the "is a" relationship is temporal / can change over time and the relationship is conditional. In this example, "This file is a file that Subversion isn't tracking" is only true for a while, until you do something that makes this no longer true.

On the other hand, when you extend a class, the "is a" relationship between the parent and child classes is unconditional, lasts much longer, and cannot be changed at runtime.

Junilu - [How to Ask Questions] [How to Answer Questions]
I agree. Here's the link:
subject: Decorator Design Pattern
It's not a secret anymore!