Extend or Decorate?

R

Roger Varley

Hi

Are there any "rules of thumb" that would help me decide whether to
extend an existing class or use the decorator pattern when adding
functionality to a base class?

Regards
 
O

Oliver Wong

Roger Varley said:
Hi

Are there any "rules of thumb" that would help me decide whether to
extend an existing class or use the decorator pattern when adding
functionality to a base class?

Regards

From
http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/DecoratorPattern.htm:

<quote>
The Decorator Pattern is used for adding additional functionality to a
particular object as opposed to a class of objects. It is easy to add
functionality to an entire class of objects by subclassing an object, but it
is impossible to extend a single object this way. With the Decorator
Pattern, you can add functionality to a single object and leave others like
it unmodified.
</quote>

- Oliver
 
I

IchBin

Roger said:
Hi

Are there any "rules of thumb" that would help me decide whether to
extend an existing class or use the decorator pattern when adding
functionality to a base class?

Regards
If you have a lot of different types of decoration you want to perform
it it much better to use the decoration pattern. Example a JTable with
three different types of decorations..

public StockMarketOrderDisplay()
{
initialise();
}

private void initialise()
{
contentPane = (JPanel)this.getContentPane();
this.setTitle("Stock Market Order Display");

// Setup Renderers;
registerRendererForClass(String.class);
registerRendererForClass(Number.class);
registerRendererForClass(Double.class);

// put in scrollpane
JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().add(table, null);
// Add scrollpane to content pane
contentPane.add(scrollPane);
}

private void registerRendererForClass(Class klass)
{
// Get Default Renderer from the table
DefaultTableCellRenderer defaultRenderer =
(DefaultTableCellRenderer)table
.getDefaultRenderer(klass);

// Wrap the color renderer around the default renderer
TableCellRenderer colorRenderer = new CellColorRenderer(
defaultRenderer, provider);

// Wrap the flash renderer around the colour renderer
TableCellRenderer flashRenderer = new CellFlashColorRenderer(
colorRenderer, flashProvider);

// Register our flash renderer with the table
table.setDefaultRenderer(klass, flashRenderer);
}

--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
M

Michael Redlich

Roger said:
Are there any "rules of thumb" that would help me decide whether to
extend an existing class or use the decorator pattern when adding
functionality to a base class?

Hi Roger:

The intent of the Decorator design pattern is to provide a flexible
alternative to subclassing for extending functionality.

To expand on what IchBin mentioned, you want to avoid *excessive*
subclassing to the point where it is difficult to predict *all*
possible common object behaviors in an abstract base class or
interface. Excessive subclassing also creates a very messy UML diagram
and makes the application that much harder to maintain.

IchBin's example demonstrated how to use an exisiting Decorator, but if
you would like to develop your own, I highly recommend the Head First
Design Patterns book by Eric & Elisabeth Freeman. The sample chapter
from O'Reilly's web site happens to be on the Decorator design pattern.
The URL is
http://www.oreilly.com/catalog/hfdesignpat/chapter/ch03.pdf.

Hope this helps along with everyone else's feedback...

Sincerely,

Mike.

--- ACGNJ Java Users Group (http://www.javasig.org/)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top