Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Extend or Decorate?

Reply
Thread Tools

Extend or Decorate?

 
 
Roger Varley
Guest
Posts: n/a
 
      01-18-2006
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

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      01-18-2006

"Roger Varley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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/JavaR...orPattern.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


 
Reply With Quote
 
 
 
 
IchBin
Guest
Posts: n/a
 
      01-18-2006
Roger Varley wrote:
> 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-)
 
Reply With Quote
 
Michael Redlich
Guest
Posts: n/a
 
      01-18-2006

Roger Varley wrote:
>
> 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/hfdes...apter/ch03.pdf.

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

Sincerely,

Mike.

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to extend the range of my MN-700 Base Station =?Utf-8?B?d29vcm9i?= Wireless Networking 1 07-02-2005 03:15 AM
Can you extend HtmlControls? Steve - DND ASP .Net 2 05-18-2004 04:47 AM
Looking for recommendation to extend DS3 over fiber Pasquale Cisco 2 01-23-2004 04:16 PM
How to Extend Session time Yuriy ASP .Net 4 10-17-2003 03:40 PM
How to extend Session? Yuriy ASP .Net 2 10-17-2003 06:39 AM



Advertisments