Decouple With Interface : Decouple « Spring « Java Tutorial






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;
  }
}
  Download:  Spring-DecoupleWithInterface.zip( 2,564 k)








28.1.Decouple
28.1.1.Spring Style Decouple
28.1.2.Decouple With Interface
28.1.3.Closed Coupled