Difference between revisions of "Decorator"
m |
(→Sample Code) |
||
Line 1: | Line 1: | ||
− | + | [[BTP600]] > [[Decorator]] | |
Allow new/additional responsibility to be added to an existing object dynamically. Decoratores provide a flexible alternative to subclassing for extending functionality. | Allow new/additional responsibility to be added to an existing object dynamically. Decoratores provide a flexible alternative to subclassing for extending functionality. | ||
Line 12: | Line 12: | ||
==Sample Code== | ==Sample Code== | ||
− | + | Beverage is the abstract component class | |
− | + | public abstract class Beverage { | |
− | + | String description = "Unknown Beverage"; | |
− | |||
− | |||
− | |||
− | |||
− | + | public String getDescription() { | |
− | + | return description; | |
− | public | ||
− | |||
} | } | ||
− | public String | + | public abstract double cost(String sz); |
− | + | //static abstract double size(); | |
− | |||
} | } | ||
− | + | Leaf classes that inherit from Beverage class | |
− | + | public class DarkRoast extends Beverage { | |
− | + | public DarkRoast() { | |
− | + | description = "Dark Roast Coffee"; | |
− | + | } | |
− | public | + | public double cost() { |
− | + | return .99; | |
} | } | ||
} | } | ||
− | + | ||
− | + | public class Decaf extends Beverage { | |
− | class | + | public Decaf() { |
− | public | + | description = "Decaf Coffee"; |
− | |||
} | } | ||
− | public | + | public double cost() { |
− | + | return 1.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; | ||
− | + | public Mocha(Beverage beverage) { | |
− | + | this.beverage = beverage; | |
} | } | ||
public String getDescription() { | public String getDescription() { | ||
− | + | return beverage.getDescription() + ", Mocha"; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
+ | } | ||
+ | |||
+ | Another concrete decorator | ||
+ | public class Vanilla extends CondimentDecorator { | ||
+ | Beverage beverage; | ||
− | public | + | public Vanilla(Beverage beverage) { |
− | + | this.beverage = beverage; | |
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
public String getDescription() { | public String getDescription() { | ||
− | + | return beverage.getDescription() + ", Vanilla"; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } |
Revision as of 12:06, 28 February 2007
Allow new/additional responsibility to be added to an existing object dynamically. Decoratores provide a flexible alternative to subclassing for extending functionality.
Introduction
Decorator, a structual pattern also known as wrapper, adds additional functionality to a class at runtime through composition. Decorators are alternative to subclassing which add behaviour at compile time. By wrapping, it allows us to add things to the component without requiring every subclass to inherit these new qualities. Each decorator class wraps a component, which means the decorator has an instance variable that holds a reference to a component.
Motivation
As an example we can look at graphical user interface toolkits where we want to add responsibility such as scrolling behaviour or border property to individual objects, not to an entire class. By using inheritance to add responsibility to a user interface component is inflexible because it is made statically. A better approach is wrapping the decorator component in another object dynamically.
Sample Code
Beverage is the abstract component class
public abstract class Beverage { String description = "Unknown Beverage"; public String getDescription() { return description; } public abstract double cost(String sz); //static abstract double size(); }
Leaf classes that inherit from Beverage class
public class DarkRoast extends Beverage { public DarkRoast() { description = "Dark Roast Coffee"; } public double cost() { return .99; } }
public class Decaf extends Beverage { public Decaf() { description = "Decaf Coffee"; } public double cost() { return 1.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; public Mocha(Beverage beverage) { this.beverage = beverage; } public String getDescription() { return beverage.getDescription() + ", Mocha"; } }
Another concrete decorator
public class Vanilla extends CondimentDecorator { Beverage beverage; public Vanilla(Beverage beverage) { this.beverage = beverage; } public String getDescription() { return beverage.getDescription() + ", Vanilla"; } }
Example
- Decorating our Beverages
- Java I/O
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. http://www.dofactory.com/Patterns/PatternDecorator.aspx