Java Reference
In-Depth Information
Only classes that are annotated with @ClearanceSale and implement the Product interface are
injected into the delegate injection point of the PriceDiscountDecorator decorator; therefore,
only your Plate class will be decorated. A delegate injection point can have as many qualii ers as is
required, and it will only be bound to beans with the same qualii er.
Decorators Without XML Coni guration
At deployment time, the CDI container scans all the JAR and WAR i les in the application looking
for bean.xml deployment descriptors. For those that it i nds, it processes each one in turn, making
the appropriate coni gurations. When it meets the <decorator/> descriptor, it enables the decorators
for the archive in which the bean.xml i le was found. It does not enable them for the whole
application. This is a problem for developers who want the decorators to apply to all classes that
implement the same interface regardless of where they are in the application. Since CDI 1.1, 4 it has
been possible to enable decorators for the entire application by annotating the decorator class with
@Priority and an Interceptor.Priority value. Here is an example of how to enable your two
decorators for the whole application.
@Priority(Interceptor.Priority.APPLICATION)
@Decorator
public class PriceDiscountDecorator extends AbstractDiscountDecorator
@Priority(Interceptor.Priority.APPLICATION+10)
@Decorator
public class BlackFridayDiscountDecorator extends AbstractDiscountDecorator
Decorators annotated with a lower value priority are called i rst. In the preceding example,
PriceDiscountDecorator is invoked before BlackFridayDiscountDecorator .
r
Decorators annotated with @Priority are called before decorators in the deployment descriptor. If
a decorator is enabled in both, it is called twice. This may lead to undesirable results, so you need to
ensure that decorators are enabled in only one way.
WHERE AND WHEN TO USE THE DECORATOR PATTERN
The decorator pattern dynamically adds behavior to an object at run time or when it is not possible
or advisable to use subclassing (perhaps because it would create multiple subclasses). The pizza
restaurant example shows how to add behavior to a pizza object at run time based on choices the
customer made.
The functionality of an application programming interface (API) can be extended and
improved by wrapping it in a decorator. Data streams are often decorated in this way. java
.io.BufferedInputStream is a good example of a decorator wrapping a lower‐level API and adding
functionality to buffer an input stream.
In Java EE, decorators are implemented via Context Dependency Injection (CDI). You can use
decorators to add new business behavior or any other functionality that can be wrapped around the
 
Search WWH ::




Custom Search