Decouple With Interface : Decouple « Spring « Java






Decouple With Interface

       
File: Main.java


public class Main {
  public static void main(String[] a) {
    MessageData source = new SimpleMessageData("Hello, world");
    MessageReporter destination = new StdoutMessageReporter();

    destination.write(source.getMessage());
  }
}

interface MessageReporter {

  void write(String message);

}

interface MessageData {

  String getMessage();

}

class StdoutMessageReporter implements MessageReporter {

  public void write(String message) {
    System.out.println(message);
  }
}

class SimpleMessageData implements MessageData {
  private final String message;

  public SimpleMessageData() {
    this("Hello, world");
  }

  public SimpleMessageData(String message) {
    this.message = message;
  }

  public String getMessage() {
    return this.message;
  }
}




           
       








Spring-DecoupleWithInterface.zip( 2,564 k)

Related examples in the same category

1.Closed Coupled
2.Spring Style Decouple
3.Spring Prototype