Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Extend or Decorate?

Thread view: 
Roger Varley - 18 Jan 2006 12:43 GMT
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
Oliver Wong - 18 Jan 2006 15:02 GMT
> Hi
>
[quoted text clipped - 3 lines]
>
> 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
IchBin - 18 Jan 2006 17:47 GMT
> Hi
>
[quoted text clipped - 3 lines]
>
> 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);
    }

Signature

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-)

Michael Redlich - 18 Jan 2006 19:19 GMT
> 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/)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.