Design patterns

Design pattern represent solutions to architectural problems related to certain contexts. PWG uses the following design patterns to build relations between the classes of a component:

Strategy

The Strategy design pattern defines a set of algorithms, encapsultes them into classes and let them to be interchangeable.

strategy class diagram

PWG uses the Strategy pattern to handle the Drawer and ContentProvider classes, that could be used by different implementations of the Widget class.

Factory method

The Factory method pattern defines an interface to create an object, but let the classes that implement the interface to specify which class to instantiate. Factory Method avoid the necessity to create dependencies between custom classes and core package classes.

factory method class diagram

PWG uses the Factory Method pattern to register custom components into a preferences file.

Template method

The Template method pattern defines the skeleton of an algorithm in a superclass operation, deferring the implementation of some of its steps to subclasses. This pattern let subclasses redefine the algorithm steps without changing the algorithm structure.

template method class diagram

PWG uses the Template method pattern into the Drawer or ContentProvider subclasses. For example, the com.pow2.webgui.tabbedselector.TabbedDrawer class implements draw() method as:

  /**
   *  Gets the string representation of this widget.
   *  <br>
   *  This method uses the TemplateMethod design pattern; it calls
   *  the following methods in the proper order:
   *  <ul>
   *    <li><code>getUpper</code><\li>
   *    <li><code>getUpperRight</code><\li>
   *    <li><code>getBorder</code><\li>
   *    <li><code>getBottom</code><\li>
   *  </ul>
   *
   *  TabbedPanel subclasses should override those methods
   *  providing custom return values.
   *
   * @return  the string representation of this widget
   * @exception Exception if any error occurs
   */
  public StringBuffer draw() throws Exception
  {
    TabbedSelector tabbedSelector = (TabbedSelector)getWidget();

    return (getUpper().
        append(tabbedSelector.getTabsDescription()).
        append(getUpperRight()).
        append(getBorder(true)).
        append(tabbedSelector.getContent()).
        append(getBorder(false)).
        append(getBottom()));
  }