Java Reference
In-Depth Information
The Manager class is said to delegate the work() operation to their direct report, and
no actual work is performed by the Manager object. Variations of this pattern
involve some work being done in the delegating class, with only some calls being
forwarded to the delegate object.
Another useful, related technique is called the decorator pattern —this provides the
capability to extend objects with new functionality, including at runtime. The slight
overhead is some extra work needed at design time. Let's look at an example of the
decorator pattern as applied to modeling burritos for sale at a taqueria. To keep
things simple, we've only modeled a single aspect to be decorated—the price of the
// The basic interface for our burritos
interface Burrito {
double getPrice ();
// Concrete implementation—standard size burrito
public class StandardBurrito implements Burrito {
private static final double BASE_PRICE = 5.99 ;
public double getPrice () {
return BASE_PRICE ;
// Larger, super-size burrito
public class SuperBurrito implements Burrito {
private static final double BASE_PRICE = 6.99 ;
public double getPrice () {
return BASE_PRICE ;
These cover the basic burritos that can be offered—two different sizes, at different
prices. Let's enhance this by adding some optional extras—jalapeño chilies and gua‐
camole. The key design point here is to use an abstract base class that all of the
optional decorating components will subclass:
* This class is the Decorator for Burrito—it represents optional
* extras that the burrito may or may not have.
public abstract class BurritoOptionalExtra implements Burrito {
private final Burrito burrito ;
private final double price ;
// This constructor is protected to protect against the default
// constructor and to prevent rogue client code from directly
// instantiating the base class.
protected BurritoOptionalExtra ( Burrito toDecorate ,
Search WWH ::

Custom Search