concept decorator in category ejb

This is an excerpt from Manning's book EJB 3 in Action, Second Edition.
CDI is a powerful solution to the problem. It provides EJB (as well as all other APIs and components in the Java EE environment) best-of-breed, next-generation, generic dependency injection and context management services. CDI features include injection, automatic context management, scoping, qualifiers, component naming, producers, disposers, registry/lookup, stereotypes, interceptors, decorators, and events. Unlike many older dependency injection solutions, CDI is completely type-safe, compact, futuristic, and annotation-driven. We’ll cover CDI in detail in chapter 12.
Related to interceptors, CDI also introduces a new construct called decorators. A decorator is an interceptor that’s tied to a specific interface. The decorator implements the interface it will be intercepting. Thus, a decorator is an interceptor that provides interception that’s tied to the specific business logic. A decorator, unlike an interceptor, has intimate knowledge of the class it’s intercepting. An interceptor is a generic solution for crosscutting across disparate classes, whereas a decorator is for crosscutting on a specific inheritance hierarchy.
A decorator is very similar to an interceptor. With a decorator you’re once again intercepting method invocations on a bean. But unlike an interceptor, you’re overriding the methods you want to intercept. A decorator extends or implements the interface of the bean it’s intercepting. As a result, a decorator is tightly coupled to the bean and can implement business logic. Because you’re overriding methods, you have easy access to method parameters.
To better understand decorators, let’s focus on the bidding in ActionBazaar. One of the major concerns with a bidding site is fraud. When you’re bidding against another user, you don’t want to be bidding against the seller who has created a fake account to drive up the price of the item. To protect against this, you need rudimentary fraud detection. Although fraud detection is a part of the bidding process, it isn’t core to the logic handling the bid. It’s a crosscutting concern similar to logging but more specific. Consequently, you’ll implement it using a decorator, as shown in the next listing.
This listing has the code for the decorator. Creating a decorator involves annotating the class with the @Decorator annotation
and implementing or extending the class you wish to decorate
. Then you inject
the class you’re decorating and mark it as the delegate instance
. You use a qualifier to ensure you get the correct instance
. Finally, you override the method you’re interested in intercepting
. Because you’re interested only in intercepting the placeBid method, you mark the class as being abstract.
CDI’s support for interceptors comes in two flavors: traditional interceptors and decorators. A traditional interceptor can be applied to any method on any bean. The interceptor isn’t specific to the bean or method it’s intercepting. A traditional interceptor is what we usually think of when we see the term interceptors. CDI also includes a new type of interceptor called a decorator. A decorator implements the interface of the class it’s intercepting—it’s thus coupled to that class and intimately tied to its implementation or business function. Thus, a decorator knows its implementation.
and implementing or extending the class you wish to decorate
. Then you inject
the class you’re decorating and mark it as the delegate instance
. You use a qualifier to ensure you get the correct instance
. Because you’re interested only in intercepting the placeBid method, you mark the class as being abstract.