Java Reference
In-Depth Information
Alternatives are often used during the testing phase of development to create mock
objects.
Stereotypes
You can think of stereotypes as templates that dei ne the characteristics of a specii c functionality
of a bean type. For example, a bean that is used at the model layer of an Model View Controller
(MVC) application requires certain annotations to perform its function. These would include the
following:
@Named
@RequestScoped
@Stereotype
@Target({TYPE, METHOD, FIELD})
@Retention(RUNTIME)
Only
@Named
and
@RequestScoped
are enough to dei ne a Model bean. Others are required to
create an annotation called
@Model
.
You could apply these annotations on every bean that requires them, or you could dei ne a
stereotype called
@Model
and apply only that to the beans. The latter makes your code much easier
to read and maintain.
To create a stereotype, you dei ne a new annotation and apply the required annotations as in
Listing 5‐13.
LISTING 5‐13:
Stereotype annotation
@Named
@RequestScoped
@Stereotype
@Target({TYPE, METHOD, FIELD})
@Retention(RUNTIME)
public @interface Model {}
Any bean annotated with
@Model
has a request scope (
@RequestScoped
) and is visible to EL
d
(
@Named
). Luckily, the CDI container that comes with this stereotype has already been dei ned.
d
A typical use of the stereotype annotation is to combine with alternative annotation so you have a
way to annotate mock objects.
Other Patterns via CDI
CDI unleashed a great power to Java EE developers. CDI goes beyond being just a simple DI
framework by making the implementation of all those patterns possible with minimal code.
The chapters that follow dive deep into details of these patterns; however, to whet your appetite,
here's a brief introduction to these CDI‐powered patterns.
Chapter 7, “Decorator Pattern,” covers the decorator pattern. Decorators wrap a target object to
dynamically add new responsibilities at run time. Each decorator can be wrapped by another, which