Java Reference
In-Depth Information
See Also
The javadoc documentation for
Exception
lists a large number of subclasses; you might
look there first to see if there is one you can use.
Using Dependency Injection
Problem
You want to avoid excessive coupling between classes, and you want to avoid excessive code
dedicated to object creation/lookup.
Solution
Use a Dependency Injection Framework.
Discussion
A Dependency Injection Framework allows you to have objects “passed in” to your code in-
stead of making you either create them explicitly (which ties your code to the implementing
class name, since you're calling the constructor) or looking for them (which requires use of a
possibly cumbersome lookup API, such as JNDI, the Java Naming and Directory Interface).
Three of the best-known Dependency Injection Frameworks are the
Spring Framework
, the
we have three classes, a Model, a View, and a Controller, implementing the traditional MVC
pattern. Given that we may want to have different versions of some of these, especially the
View, we'll define Java interfaces for simple versions of the Model and View, shown in the
following code:
Example 8-7. MVC Model Interface
public
public interface
interface
Model
Model
{
String
getMessage
();
}
MVC View Interface