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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The CDI Event: How to Achieve Success on the Open-Close Principle
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Thermometer Continuation in Scala
  • Thumbnail Generator Microservice for PDF in Spring Boot

Trending

  • Kubeflow: Driving Scalable and Intelligent Machine Learning Systems
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • Docker Model Runner: Streamlining AI Deployment for Developers
  • A Guide to Developing Large Language Models Part 1: Pretraining
  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.1K 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
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Thermometer Continuation in Scala
  • Thumbnail Generator Microservice for PDF in Spring Boot

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!