Java Reference
In-Depth Information
allows for a theoretically unlimited number of decorated target objects at run time. The decorator
pattern uses the @Decorator and the @Delegate annotations. You must specify the decoration order
in beans.xml .
The factory pattern is covered in Chapter 6. Factories minimize the usage of the new keyword and
can encapsulate the initialization process and different concrete implementations. The factory
pattern uses the @Produces annotation to mark producer methods. Target object can inject or
observe the produced objects.
The observer pattern and events are addressed in Chapter 11, “Observer Pattern.” The observer
pattern changes the direction of a message, thus the order of caller and the callee. With the help
of the observer pattern, instead of aggressively checking a resource, an object can subscribe to
the changes on the resource. The observer pattern in Java EE uses the @Observes annotation and
events. The target observer(s) can observe any i red event.
Aspects and interceptors are the focus of Chapter 8, “Aspect‐Oriented Programming (Interceptors).”
They let you change execution l ow at run time. Any aspect or interceptor can be marked to cut the
execution and kick in at the given point. This approach enables dynamic changes even on a large
code base.
SUMMARY
In this chapter, you have seen the concept of dependency injection in Java EE. The dependency
injection concept lets us build loosely coupled systems easier than one can ever imagine. We have
seen how dependency injection allows us to eliminate the use of a new keyword, thus, manual object
creation.
We also focused on CDI, which unleashes a huge potential by leveraging a whole new container.
With the help of CDI, dependency injection can be applied to any object and many patterns that are
discussed in this topic are easy to implement.
EXERCISES
1. Design a service class which will return some string to the client.
2. Implement a i le reader and inject it to the service you developed before.
3. This time implement an object which reads the html content as a string from a i xed URL.
4. Think about what you need to refactor in the service class to be able to inject both data
providers with the same reference.
5. Is there a way to dynamically inject each implementation depending on different circumstances?
For example can you make sure the i le reader is injected during development but the http
reader is used during production?
 
Search WWH ::




Custom Search