Changes

Jump to: navigation, search

Decorator

3,926 bytes added, 14:33, 11 April 2007
m
Example
}
[[Image:wrappingdecoratorwrappingdecorator2.png|300 px|thumb|Figure 1 wrapping Decorator classes]]
Leaf classes that inherit from Beverage class
==Example==
[[Image:javaIOdecoratorjavaIOdecorator2.png|500 px|thumb|Java I/O classes]]
Java I/O [http://java.sun.com/javase/6/docs/api/java/io/package-frame.html API]
----
Abstract Component class:
public abstract class InputStream {
abstract int read();
}
Concrete Component class:
class FileInputStream extends InputStream
{
public int read();
}
Decorator class:
public
class FilterInputStream extends InputStream {
{
//<span style="color:red">Local variable for storing the reference of the concrete component class</span>
protected volatile InputStream in;
protected FilterInputStream(InputStream in) { this.in = in; }
public int read() {
return in.read();
}
}
Concrete Decorator class:
public
class PushbackInputStream extends FilterInputStream {
public PushbackInputStream(InputStream in, int size) {
super(in);//<span style="color:red">Calls the FilterInputStream class and stores the reference of InputStream</span>
if (size <= 0) {
throw new IllegalArgumentException("size <= 0");
if (pos < buf.length) {
return buf[pos++] & 0xff;
} return super.read(); } } ---- [[Image:mpclDecorator.png|400 px|thumb|MPCL]] Multi purpose class library (MPCL) [http://www.google.com/codesearch?hl=en&q=show:QUdC7Hs10WU:9dcfaUrTzRs:gcPYjQUkI_I&sa=N&ct=rd&cs_p=http://www.uesqlc.org/download/mpcl/mpcl-11.0.2.zip&cs_f=mpcl-11.0.2/src/java/lib/org/mpcl/nui/table/TDecoratorTableCellRenderer.java Source Code] GPL license ---- Concrete Components <br>TDecoratorTableCellRenderer: /** * Table cell renderer that decorates a delegate cell renderer. It implements * the Decorator design pattern. */ public class TDecoratorTableCellRenderer implements TableCellRenderer { /// Delegate table cell renderer. private TableCellRenderer tDelegateTableCellRenderer; /// Table cell decorator. private ITableCellDecorator tTableCellDecorator; // // C O N S T R U C T O R S // /** * Builds a new instance. * @param tDELEGATE_TABLE_CELL_RENDERER The delegate table cell renderer. * @param tTABLE_CELL_DECORATOR Table cell decorator. */ public TDecoratorTableCellRenderer ( TableCellRenderer tDELEGATE_TABLE_CELL_RENDERER , ITableCellDecorator tTABLE_CELL_DECORATOR ) { tDelegateTableCellRenderer = tDELEGATE_TABLE_CELL_RENDERER; tTableCellDecorator = tTABLE_CELL_DECORATOR; }  DefaultTableCellRenderer: public class DefaultTableCellRenderer extends JLabel implements TableCellRenderer, Serializable { /** * Creates a default table cell renderer. */ public DefaultTableCellRenderer() { super(); setOpaque(true); setBorder(getNoFocusBorder());
}
}
 
TDateTableCellRenderer:
public class TDateTableCellRenderer extends DefaultTableCellRenderer
{
/// Date format.
private DateFormat tDateFormat;
//
// C O N S T R U C T O R S
//
/**
* Sets the String object for the cell being rendered to value.
* @param tVALUE The value for this cell; if \a tVALUE is \a null it sets the
* the text value to an empty string.
* @see javax.swing.table.DefaultTableCellRenderer#setValue()
*/
protected void setValue (Object tVALUE)
{
setText (( tVALUE == null ) ? "" : tDateFormat.format ((Date) tVALUE));
}
 
TPopulationTableCellRenderer:
/// Table cell renderer for \a TPopulation objects.
public class TPopulationTableCellRenderer extends DefaultTableCellRenderer
{
/// Number format.
public NumberFormat tNumberFormat;
//
// C O N S T R U C T O R S
//
/**
* Sets the String object for the cell being rendered to value.
* @param tVALUE The value for this cell; if \a tVALUE is \a null it sets the
* the text value to an empty string.
* @see javax.swing.table.DefaultTableCellRenderer#setValue()
*/
protected void setValue (Object tVALUE)
{
if ( tNumberFormat == null)
{
tNumberFormat = TDecimalFormat._getPopulationInstance();
}
setText (( tVALUE == null ) ? "" : tNumberFormat.format (((TPopulation) tVALUE).intValue()));
}
//
// C O N S T R U C T O R S
//
/// Builds a new instance.
public TPopulationTableCellRenderer()
{
super();
setHorizontalAlignment (DefaultTableCellRenderer.RIGHT);
}
} // class TPopulationTableCellRenderer
 
==Contributions==
[http://zenit.senecac.on.ca/wiki/index.php/Talk:Adapter Adapter pattern] in Eclipse
==References==
Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. ISBN 0-201-63361-2.
 
Freeman, Eric; Freeman, Elisabeth; Sierra, Kathy; Bates, Bert (2004). Head First Design Patterns. O`Reilly. ISBN 0-596-00712-4.
 
http://www.dofactory.com/Patterns/PatternDecorator.aspx
1
edit

Navigation menu