AMZ
AMZ

Reputation: 362

Why do we inherit from the component class in decorator pattern?

In the decorator pattern, the decorator class inherits from the component class and has an aggregation relationship with it. Well, the aggregation relationship is logical, but why should we inherit from the component? This pattern is supposed to decorate the component, so why does it need to inherit from it? If we have a component object in the decorator, we can add new functionalities to our class at runtime as we want, but I don't understand the inheritance.

uml photo

Upvotes: 0

Views: 30

Answers (1)

Mike Mozhaev
Mike Mozhaev

Reputation: 2415

Decorator should implement the interface, but it doesn't have to inherit from the specific implementation. We should be able to use the decorator instead of the decorated component, but we should have a dependency on interface, not the specific class.

Upvotes: 1

Related Questions