Java Reference
In-Depth Information
public
public interface
interface
View
View
{
void
void
displayMessage
();
}
The implementations of these are not shown, because they're so trivial, but they are online.
The Controller in this example is a main program, no interface needed. First, a version of the
main program
not
using Dependency Injection. Obviously the View requires the Model, to
get the data to display:
ControllerTightlyCoupled.java
public
public class
class
ControllerTightlyCoupled
ControllerTightlyCoupled
{
public
public static
void
main
(
String
[]
args
) {
Model m
=
new
static
void
new
SimpleModel
();
View v
=
new
new
ConsoleViewer
();
((
ConsoleViewer
)
v
).
setModel
(
m
);
v
.
displayMessage
();
}
}
Here we have four tasks to undertake:
1. Create the Model.
2. Create the View.
3. Tie the Model into the View.
4. Ask the View to display some data.
Now a version using Dependency Injection:
Spring Controller.java
public
public class
class
Controller
Controller
{
public
public static
void
main
(
String
[]
args
) {
ApplicationContext ctx
=
new
static
void
new
AnnotationConfigApplicationContext
(
"di.spring"
);
View v
=
ctx
.
getBean
(
"myView"
,
View
.
class
);