obtained means changing the code in the renderer. Add to this the fact that we still cannot change the
renderer easily; doing so means changing the class that launches the application.
If we take this application a step further, away from the basics of "Hello World!" a better solution is
to refactor the rendering and message retrieval logic into separate components. Plus, if we really want to
make our application flexible, we should have these components implement interfaces and define the
interdependencies between the components and the launcher using these interfaces.
By refactoring the message retrieval logic, we can define a simple MessageProvider interface with a
single method, getMessage(), as shown in Listing 2-3.
Listing 2-3. The MessageProvider Interface
package com.apress.prospring3.ch2;
public interface MessageProvider {
public String getMessage();
}
As you can see in Listing 2-4, the MessageRenderer interface is implemented by all components that
can render messages.
Listing 2-4. The MessageRenderer Interface
package com.apress.prospring3.ch2;
public interface MessageRenderer {
public void render();
public void setMessageProvider(MessageProvider provider);
public MessageProvider getMessageProvider();
}
As you can see, the MessageRenderer interface has a method, render(), and also a JavaBean-style
property, MessageProvider. Any MessageRenderer implementations are decoupled from message
retrieval and delegate it instead to the MessageProvider with which they are supplied. Here,
MessageProvider is a dependency of MessageRenderer. Creating simple implementations of these
interfaces is easy (see Listing 2-5).
Listing 2-5. The HelloWorldMessageProvider Class
package com.apress.prospring3.ch2;
public class HelloWorldMessageProvider implements MessageProvider {
public String getMessage() {
return "Hello World!";
}
}
In Listing 2-5, you can see that we have created a simple MessageProvider that always returns "Hello
World!" as the message. The StandardOutMessageRenderer class (shown in Listing 2-6) is just as simple.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home