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
Newsletter
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
  • Share Delta Lake Tables Between Databricks and Snowflake
  • Designing Memory Architecture for Stateful AI Agents: Short-Term Context and Long-Term Persistence
  • Moving 900+ GB of Prometheus Without a Cutover Weekend

Trending

  • PWA on iOS, Part 1: PWAs on the iPhone Are Older Than They Look
  • AI Agents and Frontier Models: Insights From The Neuron
  • Introducing senro: A Pipeline Engine You Program in Go
  • The Twelve-Factor Agents: Building Production-Ready LLM Applications

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
DZone Core CORE ·
Sep. 05, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Share Delta Lake Tables Between Databricks and Snowflake
  • Designing Memory Architecture for Stateful AI Agents: Short-Term Context and Long-Term Persistence
  • Moving 900+ GB of Prometheus Without a Cutover Weekend

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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