I'm trying to teach myself some design patterns using a book that I was recommended, Head First Design Patterns. I came to the chapter on the Decorator Pattern, and although I understand the purpose of it, I'm a bit fuzzy on it's layout.
Here is their basic UML of the Decorator pattern: Imgur (Sorry for the link, but SE won't let me upload a photo here for some reason)
Here is how they wrote out the code:
public abstract class Component {
String description = "Unknown Description";
public String getDescription() {
return description;
}
public abstract double cost();
}
public class ConcreteComponent extends Component {
public ConcreteComponent() {
description = "I am a concrete component";
}
public double cost() {
return 1.99;
}
}
public abstract class Decorator extends Component {
public abstract String getDescription();
}
public class ConcreteDecorator extends Decorator {
Component component;
public ConcreteDecorator(Component component) {
this.component = component;
}
public String getDescription() {
return component.getDescription() + " with a decorator";
}
public double cost() {
return .20 + component.cost();
}
}
My question is, what is the need for a Decorator class in this situation? Why not leave getDescription() abstract and let ConcreteComponents and ConcreteDecorators implement it the way they need?
As a follow up, are there actual benefits to creating a Decorator class? This example doesn't seem to cover why it's useful or how it could be useful.