Programming

Archiving Composition Over the Heritage With CDI Decorator and Delegator

Spread the love


Composite over inheritance is an OOP design principle we’ve followed for several years. Like me, you might have seen it in the Effective Java book, and we have wanted to pursue it since then. 

The inheritance brings several pitfalls, such as maintainability and a clear code. Beyond the semantics, the sample that I love to use is the cake that needs Salt. It does not make sense for a Cake to extend a Sea only because it needs Salt. In this case, it is a much better composition as well. 

public class Sea {
protected Salt salt;
}
public class Cake extends Sea {
}

On the CDI, we can avoid the inheritance in several moments with the Decorator and the interceptor.

The first step is the Decorator, which creates a structure to attach new behaviors to objects. In the Java world, we’re familiar with several samples included in the core API, such as the numbers wrappers: Integer, Float, Double, and so on.

Give a smooth sample where we have a Worker interface with a unique method to deliver a Job.

public interface Worker {

    String work(String job);
}

The next step is to create an implementation such as a regular Worker as a programmer. Let’s use us in this case, so let’s start a Programmer class that implements a Worker.

public class Programmer implements Worker {
    @Override
    public String work(String job) {
        return "converting coffee into code, the job: " + job;
    }
}

Yeap, our Programmer class, is ready to receive a job and convert coffee into code. But only a programmer is not enough. Usually, we have someone who will curate this job, define priority, and so on.

To add this new behaviour  of curacy of the job and define the priority, we’ll decorate a Programmer with a Manager, who will check if it makes sense and then delegates it to the programmer.

public class Manager implements Worker{


    private Worker worker;

    @Override
    public String work(String job) {
        return "A manager has received the job and it will delegate to a programmer -> "
                + worker.work(job);
    }
}

Yeap, we have our decorator pattern implemented, but how to guarantee that it will work will go through this manager? That is the easiest part! We have CDI. We need to use a couple of annotations to make it possible.

@Decorator
@Priority(Interceptor.Priority.APPLICATION)
public class Manager implements Worker{

    @Inject
    @Delegate
    @Any
    private Worker worker;

    @Override
    public String work(String job) {
        return "A manager has received the job, and it will delegate to a programmer -> "
                + worker.work(job);
    }
}

As the code above, we put the annotation to define a Manager as a Decorator, and it will inject any Worker as a delegate.

The next step is with an interceptor, as the name already explains. It will intercept a method or methods defined by an annotation. 

Such as a scenario where we want to, when a method executes, open a connection and then commit an operation on success, but do a rollback when it returns an exception. With a CDI interceptor, it will happen quickly.

In this video, we’ll explore the power of interceptor and Decorator to make it easier for you to apply the Composition over inheritance principle using this powerful CDI engine.



Source link

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button