The purpose or motivation of the decorator pattern is to avoid inheritance and use associations instead. The reason is that successive layers of content will become more and more, threatening to get out of control. In terms of extensibility, association is better than inheritance. The so-called association, A uses B, is called A association with B.

Component Abstract

ConcreteComponent ConcreteComponent

Decorator Abstracts the Decorator class

As you can see, the Decorator aggregates Component, which has the same operation() method as the Decorator. The Decorator itself implements its own operation(dependency injection) by calling Component.

In practice, we program to the Decorator interface; If extended, extend ConcreteComponent operation(). The interface doesn’t change, so our application doesn’t have to change anything.