I am just starting to learn design patterns and I have two questions related to the Decorator...
I was wondering why the decorator pattern suggests that the decorator implement all public methods of the component of which it decorates?
Can't the decorator class just be used to provide the additional behaviors, and then the concrete component (which is passed into it) just be used to call everything else?
And secondly, what if the concrete component you want to decorate doesn't have a base class which the abstract decorator can also derive from?
Thanks in advance!
I think you have have misunderstood Decorator. You're thinking of a simple case of extending a concrete class with additional functionality. In this case, yes in most OO languages the derived class can simply allow its superclass to handle any unimplemented methods.
A Decorator class does not extend the base class of its "decorated" class. It is a different type, which has a member object of the decorated class. Thus it must implement the same interface, if only to call the respective method of the decorated object.
It might be worthwhile to define an interface (if your language supports such a thing) for both the decorated class and the Decorator class. That way you can check at compile time that the Decorator implements the same interface.
Re: @Yossi Dahan's comment: I see the ambiguity in the wikipedia article, but if you read carefully it does say that the component being decorated is a field in the decorator object, and that the component is passed as an argument to the decorator constructor. This is different from inheritance.
Though the wikipedia article does say the decorator inherits from the component, you should think of this as implementing an interface, as I showed in the PHP example above. The decorator still has to proxy for the component object, which it wouldn't if it had inherited. This allows the decorator to decorate an object of any class that implements that interface.
Here are some excerpts from "Design Patterns: Elements of Reusable Object-Oriented Software" by Gamma, Helm, Johnson, and Vlissides:
A decorator should be a drop in replacement for the component it decorates, with extra functionality (the "decoration"). This can only happen if it completely implements the component's interface.
That makes assumptions on how the decorator is implemented. You can't be sure that the entire public interface of the component it decorates is being passed through directly. It's possible that the decorator overrides certain methods in its implementation.
Decorators generally inherit the component's class they are decorating, not the component's base.