Changes

Jump to: navigation, search

Decorator

901 bytes removed, 12:06, 28 February 2007
Sample Code
[[BTP600]] > [[Decorator]]
Allow new/additional responsibility to be added to an existing object dynamically. Decoratores provide a flexible alternative to subclassing for extending functionality.
==Sample Code==
window/scrolling scenario  // Beverage is the Window interfaceabstract component class interface Window public abstract class Beverage { public void draw(); // draws the Window public String getDescription()description = "Unknown Beverage"; // returns a description of the Window }
// implementation of a simple Window without any scrollbars class SimpleWindow implements Window { public void drawString getDescription() { // draw window return description;
}
public abstract double cost(String getDescription(sz) { return "simple window"; }//static abstract double size();
}
The decorator Leaf classes  // abstract decorator that inherit from Beverage class - note that it implements Window abstract public class WindowDecorator implements Window DarkRoast extends Beverage { protected Window decoratedWindowpublic DarkRoast() { description = "Dark Roast Coffee"; // the Window being decorated }
public WindowDecorator double cost(Window decoratedWindow) { this return .decoratedWindow = decoratedWindow99;
}
}
// the first concrete decorator which adds vertical scrollbar functionality public class VerticalScrollBarDecorator Decaf extends WindowDecorator Beverage { public VerticalScrollBarDecorator Decaf(Window decoratedWindow) { super(decoratedWindow) description = "Decaf Coffee";
}
public void drawdouble cost() { drawVerticalScrollBar(); decoratedWindow return 1.draw()05;
}
}
 
Condiment Decorator class
public abstract class CondimentDecorator extends Beverage {
public abstract String getDescription();
}
 
Mocha is a concrete decorator class that implements cost() and getDescription()
public class Mocha extends CondimentDecorator {
Beverage beverage;
private void drawVerticalScrollBarpublic Mocha(Beverage beverage) { // draw the vertical scrollbar this.beverage = beverage;
}
public String getDescription() {
return decoratedWindowbeverage.getDescription() + ", including vertical scrollbarsMocha"; } } // the second concrete decorator which adds horizontal scrollbar functionality class HorizontalScrollBarDecorator extends WindowDecorator { public HorizontalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow);
}
}
 
Another concrete decorator
public class Vanilla extends CondimentDecorator {
Beverage beverage;
public void drawVanilla(Beverage beverage) { drawHorizontalScrollBar(); decoratedWindow this.draw()beverage = beverage; } private void drawHorizontalScrollBar() { // draw the horizontal scrollbar
}
public String getDescription() {
return decoratedWindowbeverage.getDescription() + ", including horizontal scrollbarsVanilla"; } } main program public class DecoratedWindowTest { public static void main(String[] args) { // create a decorated Window with horizontal and vertical scrollbars Window decoratedWindow = new HorizontalScrollBarDecorator ( new VerticalScrollBarDecorator(new SimpleWindow())); // print the Window's description System.out.println(decoratedWindow.getDescription());
}
}
1
edit

Navigation menu