Execution result:
To run the example, execute the "examples0.bat" file that is included with the source code.
For a simple program, the above program may be OK but as the application becomes more complex, it may be necessary to change the message.
If the message changes often, it may become benefitial to create an interface and have the client program choose which implementation of the interface to use. That is, which message to output.
Greeting interface just contains a greet() method which will be invoked from the application.
(Greeting.java)
public interface Greeting {
public void greet(); // method to output message
}
GreetingImpl is one implementation of Greeting. To differentiate it from other implementation of Greeting, we'll output "1 Hello World!".
(GreetingImpl.java)
public class GreetingImpl implements Greeting {
public String greet() {
return "2 Hello World!";
}
}}
Greeting2Impl is other implementation of Greeting. In this implementation, we'll output "1b Hello World!" to distinguish it.
(Greeting2Impl.java)
public class Greeting2Impl implements Greeting {
public void greet() {
System.out.println("1b Hello World!"); // output message
}
}
Finally, an application to call the implementations. We'll create both implementation. Note that both greeting and greeting2 are of type Greeting even though their implementation differ. Using the Greeting interface allows us to use variable "greeting" for both GreetingImpl and Greeting2Impl.
(HelloWorld1.java)
public class HelloWorld1 {
public static void main(String[] args) {
Greeting greeting = new GreetingImpl(); // use GreetingImpl
greeting.greet(); // output message
greeting = new Greeting2Impl(); // use Greeting2Impl
greeting.greet(); // output message
}
}
Execution result:
To run the example, execute the "examples1.bat" file that is included with the source code. Note that "greeting" was first set to "GreetingImpl" to output "1 Hello World!" and then changed to "Greeting2Impl" to output "1b Hello World!".
We were able to change the message but what if we also want to change where the message is outputted? Let's try to change the above example so we'll also be able to change the output to System.err as well as System.out.
We'll make the output routine into an interface - GreetingClient.
(GreetingClient.java)
public interface GreetingClient {
void setGreeting(Greeting greeting); // set message to output
void execute(); // output message
}
First, an implementation to output to System.out:
(GreetingClientImpl.java)
public class GreetingClientImpl implements GreetingClient {
private Greeting greeting;
public void setGreeting(Greeting greeting) { // set message to output
this.greeting = greeting;
}
public void execute() { // output message to System.out
System.out.println(greeting.greet());
}
}
And an implementation to output to System.err.
(GreetingClient2Impl.java)
public class GreetingClient2Impl implements GreetingClient {
private Greeting greeting;
public void setGreeting(Greeting greeting) { // set message to output
this.greeting = greeting;
}
public void execute() { // output message to System.err
System.err.println(greeting.greet());
}
}
(HelloWorld2.java)
public class HelloWorld2 {
public static void main(String[] args) {
Greeting greeting = new GreetingImpl(); // set message to output
// set how to output message
GreetingClient greetingClient = new GreetingClientImpl();
greetingClient.setGreeting(greeting); // set message to output
greetingClient.execute(); // output message
greeting = new Greeting2Impl(); // set message to output
// set how to output message
greetingClient = new GreetingClient2Impl();
greetingClient.setGreeting(greeting); // set message to output
greetingClient.execute(); // output message
}
}
Execution result:
To run the example, execute the "examples2.bat" file that is included with the source code. When the program is executed from the console, there doesn't seem to be any difference between the two implementations to output the messages, but when it is executed from Eclipse, they will be displayed in different color.
Move to Moving on to Seasar to find how we could further make the "Hello World!" program better.