Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decorator pattern: Why do we need an abstract decorator? [duplicate]

This question was asked already here, but rather than answering the specific question, descriptions of how the decorator pattern works were given instead. I'd like to ask it again because the answer is not immediately evident to me just by reading how the decorator pattern works (I've read the wikipedia article and the section in the book Head First Design Patterns).

Basically, I want to know why an abstract decorator class must be created which implements (or extends) some interface (or abstract class). Why can't all the new "decorated classes" simply implement (or extend) the base abstract object themselves (instead of extending the abstract decorator class)?

To make this more concrete I'll use the example from the design patterns book dealing with coffee beverages:

  • There is an abstract component class called Beverage
  • Simple beverage types such as HouseBlend simply extend Beverage
  • To decorate beverage, an abstract CondimentDecorator class is created which extends Beverage and has an instance of Beverage
  • Say we want to add a "milk" condiment, a class Milk is created which extends CondimentDecorator

I'd like to understand why we needed the CondimentDecorator class and why the class Milk couldn't have simply extended the Beverage class itself and been passed an instance of Beverage in its constructor.

Hopefully this is clear...if not I'd simply like to know why is the abstract decorator class necessary for this pattern? Thanks.

Edit: I tried to implement this, omitting the abstract decorator class, and it seems to still work. Is this abstract class present in all descriptions of this pattern simply because it provides a standard interface for all of the new decorated classes?

like image 547
oym Avatar asked Jan 06 '10 22:01

oym


People also ask

Why do we need decorator pattern?

The Decorator Pattern allows class behavior to the decorated dynamically. It's a structural design pattern as it's used to form large object structures across many disparate objects. The concept of decorator is that it adds additional attributes to an object dynamically.

Why do we use Decorator design pattern to decorate existing entities?

Decorator design pattern allows you to add functionalities to an object without subclassing, the emphasis in Composite design pattern is on representation rather than decoration. The decorator pattern enables you to modify or extend existing functionality at runtime.

When would you use the Decorator design pattern Choose 2?

The decorator pattern can be used to make it possible to extend (decorate) the functionality of a certain object at runtime.

What problem is solved by the decorator pattern?

The problem of inheritance can be solved by using a decorator pattern where you can add functionality to an object at run time.

How to understand decorator pattern?

To understand decorator pattern let us consider a scenario inspired from the book “Head First Design Pattern”. Suppose we are building an application for a pizza store and we need to model their pizza classes. Assume they offer four types of pizzas namely Peppy Paneer, Farmhouse, Margherita and Chicken Fiesta.

Why do we use each decorator of the same type?

Each decorator is of the same type as the enclosed component and, therefore, has the same interface. In this way, incoming method calls can be easily delegated to the linked component while a functionality is being carried out. Calls can also be directly processed within the decorator. What’s the purpose of a decorator design pattern?

What is decorator pattern in Java?

This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact. We are demonstrating the use of decorator pattern via following example in which we will decorate a shape with some color without alter shape class.

What are the downsides of using the decorator pattern?

The less serious downside of the pattern is that code is simply less readable and harder to comprehend when closely related logic gets spread to different classes. This is especially the case when these classes are then composed together behind the scenes, as they are with the decorator pattern.


1 Answers

Better one and a half year late than never:

A base class for decorators of a certain interface is not necessary.

However, it is very useful to have:

  • for one thing, as a means of documenting that classes derived from it are decorators of the interface in question

  • but mostly, because decorators usually do not need to add functionality to every single method of the decorated interface.

So, a base decorator class allows derived decorators to implement only those methods of the interface to which they actually need to add some functionality, leaving the rest of the methods to the base class to provide a default implementation for. (Which simply delegates the call to the decoree.)

Contrast this with writing decorators that implement the decorated interface from scratch, where the compiler requires that you provide an implementation for every single method of the interface, whether your decorator will be adding any functionality to it, or not.

It is that simple, really.

like image 99
Mike Nakis Avatar answered Sep 21 '22 22:09

Mike Nakis