DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The CDI Event: How to Achieve Success on the Open-Close Principle
  • Token Attribution Framework for Agentic AI in CI/CD
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • Ingesting Fixed-Width Mainframe Files Into Delta Lake: The Details Nobody Writes Down

Trending

  • Identity in Action
  • Implementing Observability in Distributed Systems Using OpenTelemetry
  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  1. DZone
  2. Data Engineering
  3. Data
  4. Archiving Composition Over the Heritage With CDI Decorator and Delegator

Archiving Composition Over the Heritage With CDI Decorator and Delegator

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.

By 
Otavio Santana user avatar
Otavio Santana
DZone Core CORE ·
Sep. 05, 22 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
7.4K Views

Join the DZone community and get the full member experience.

Join For Free

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. 

Java
 
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.

Java
 
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.

Java
 
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.

Java
 
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.

Java
 
@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.

CDI Data Types

Opinions expressed by DZone contributors are their own.

Related

  • The CDI Event: How to Achieve Success on the Open-Close Principle
  • Token Attribution Framework for Agentic AI in CI/CD
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • Ingesting Fixed-Width Mainframe Files Into Delta Lake: The Details Nobody Writes Down

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook