The Java Decorator Pattern falls into the category of structural pattern and is meant to be used to dynamically extend the behavior of classes, overcoming the static nature of Java inheritance. The following section explains briefly the involved design.
The Java Decorator Pattern
The following picture shows how the Java Decorator Pattern works. An abstract decorator class is defined which extends the class that we want to “decorate”. Besides extending it, it also uses an internal instance of that class to implement all the non-abstract, overridden methods. That is to say, each of the non-abstract, overridden methods only executes the corresponding method of the parent class without doing anything else. The abstract methods then are implemented by the concrete class.
Using the above schema, we can assign the concrete decorator to any variable whose type is ToDecorateClass. This way wherever the variables with ToDecorateClass type are used, the extended behavior is in place.
The Decorator Pattern allows us to customize the functionalities of an existing piece of software in a dynamic way. In the following article, hosted by dzone.com, a possible use case is shown: https://dzone.com/articles/decorator-pattern-to-solve-messy-scenarios.