Decorator pattern is largely used in many development scenarios, enabling developers to create more maintainable code and to extend application functionality without deep impact to the entire project. Every time we think a structural pattern we think to an Adapter or a Bridge pattern. Decorator pattern solves same problems but for other domains: it is a valid alternative to functionality extension in terms of type inheritance.
Use of Decorator pattern can be found in the Java IO API where every Input or Output stream is decorated with new functionality and wrapped in a new type of Stream. In the same way we can extend code with this pattern when we need to add new beahvior to application functionalites or add a new functionality behavior.

Decorator pattern components

Every time you have to implement a decorator pattern the components that plays around are the Interface or the functionality contract, the basic behavior of that funcionality and the behavior we want extends to the basic functionality. In Java this can be translated to the extends keyword of the java class that implements a java interface. The Decorator component extends the functionality of the basic component, in Java this can be done through "extends" keyword. Extends keyword in its java technical acception (or more simply inheritance) is not always suitable specially if we have to extend a large set of methods and every method has a variety of extensions to implement. To keep the "Open Closed Principle" of software design it's more nice to look at functionality extensions in tems of compositon. So the basic component can be used as the delegate of the decorator component and the decorator component can be used as primary service as we want to use the decorated behavior. All these components share the interface in terms of what they have to do. In Spring this can be done in a elegant way through Spring Annotations, let's see a very simple example...

A Simple Spring Example

To show you how we can implement with Spring the decorator pattern you have to know what is Autowiring and some of Spring Annotations like @Component, @Autowired and @Primary. If you want to see the example in action you have to know Spring Boot, but you are free to use other containers.
So suppose to have this dummy interface:

public interface IExampleService {

    public String startExample();


And its basic realization:

public class ExampleService implements IExampleService {

    public String startExample() {
        return "Example started";

If you are watching the @Component annotation you are on the right way, you can see we annotated it with a @Component annotation with a specific value, except of this feature the service can be used as a normal Spring service.

Now suppose to extend the startExample method with a further presentation message and in the same time use the basic startExample method, the basic approach could be to extend the ExampleService class. Another more elegant approach could be to incapsulate the basic ExampleService class in a ExampleServiceDecorator class. ExampleServiceDecorator will implements the same interface to be sure we are extending the same methods, it's important to keep the various interface implementations separated in order to keep the system decoupled. Spring help us to keep the system decoupled: let's see how. The class shown below represents the DecoratorService:

public class ExampleServiceDecorator implements IExampleService {

    private IExampleService delegate;

    public String startExample() {
        String started = delegate.startExample();
        return started + " and decorated with decorator example";


as we can see this class uses the IExampleService interface but it doesn't know what kind of implementation we want to use. The relevant infos we write are that it has to use a "delegate" component of type IExampleService. Spring will follow this info to inject the delegate component. Remember that previously we annotated the basic service with the "delegate" string in the @Component value .

Finally we can use the EaxmpleDecoratorService as follow:

public class PrimaryServiceApp implements CommandLineRunner {

    private IExampleService primaryService;

    public void run(String... args) throws Exception {



    public static void main(String[] args) {, args);


In this example we are invoking th IExampleService startMethod() with its decorated implementation, so we are using the starExample() extended functionality without inheritance. The important thing is that every class doesn't know what kind of IExampleService it's using. Furthermore it'is possible to inject the extended functionality as default service because we have annotated the ExampleDecoratorService with @Primary annotation.

It's possible to create more decorators with the same approach but you have to explicitly inject a named component with @Qualifier annotation to annotate a generic decorator beahavior implementation.


I just used this pattern to decorate a Mapper service component that is reponsible to map a bean properties to another bean, but there are many fields of use of the decorator pattern so don't be scared to decorate too much your application :). If you want to see the entire working example you can check out my repository, hope it will be useful.

  • submit to reddit
blog comments powered by Disqus