Extend or Decorate?

Discussion in 'Java' started by Roger Varley, Jan 18, 2006.

  1. Roger Varley

    Roger Varley Guest

    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
     
    Roger Varley, Jan 18, 2006
    #1
    1. Advertising

  2. Roger Varley

    Oliver Wong Guest

    "Roger Varley" <> wrote in message
    news:...
    > 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
     
    Oliver Wong, Jan 18, 2006
    #2
    1. Advertising

  3. Roger Varley

    IchBin Guest

    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-)
     
    IchBin, Jan 18, 2006
    #3
  4. 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/hfdesignpat/chapter/ch03.pdf.

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

    Sincerely,

    Mike.

    --- ACGNJ Java Users Group (http://www.javasig.org/)
     
    Michael Redlich, Jan 18, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Umut Tezduyar
    Replies:
    0
    Views:
    423
    Umut Tezduyar
    Nov 22, 2005
  2. Thomas Philips
    Replies:
    2
    Views:
    336
    Paul McGuire
    Jun 23, 2004
  3. Tom Anderson
    Replies:
    1
    Views:
    448
    Benji York
    Aug 3, 2005
  4. Replies:
    10
    Views:
    624
    Xah Lee
    Sep 8, 2006
  5. Replies:
    18
    Views:
    518
    Xah Lee
    Sep 8, 2006
Loading...

Share This Page