Creating the Source Files in the Exploded Modules – Java Module System

Creating the Source Files in the Exploded Modules

The classes that are in packages included in the modules are declared in their respective source files. Example 19.1 shows the source files for the adviceApp application.

The source code of the AdviceModel.java file in the model module is shown at (1) in Example 19.1. This source file should reside in the package com.passion.model in the model module. Its file path src/model/com/passion/model/AdviceModel.java in the exploded module is shown as a comment at (1). The class AdviceModel is declared to be in package com.passion.model. It keeps track of the current advice (the String field currentAdvice). Its constructor creates an AdviceModel with the field current-Advice initialized to the string “No advice.” The method setCurrentAdvice() sets the field currentAdvice to the advice corresponding to its int parameter value. The method getCurrentAdvice() returns the value of the field currentAdvice.

The source code of the AdviceView.java file in the view module is shown at (2) in Example 19.1. This source file should reside in the package com.passion.view in the view module. Its file path src/view/com/passion/view/AdviceView.java in the exploded module is shown as a comment at (2). The class AdviceView is declared to be in package com.passion.view. It uses an AdviceModel. This model is passed to an AdviceView in the constructor. The method updateAdviceDisplay() accesses its model for the current advice and prints it.

Analogously, the source code of the AdviceController.java file in the controller module is shown at (3) in Example 19.1. An AdviceController uses an AdviceModel and an AdviceView, both of which are created in the constructor, where the Advice-Model is injected into the AdviceView. The method showAdvice() calls the AdviceModel to set the current advice based on its int parameter value, followed by a call to the AdviceView to update the advice display.

Finally, the source code of the Main.java file in the main module is shown at (4) in Example 19.1. The Main class is the entry point of the application. It simulates a user by calling an AdviceController several times to show different advice.

Note that all source code should be explicitly contained in packages, except for the module declaration—that is, the module-info.java file, which is always located immediately under the module root directory. The name of the module in the module declaration must match the name of the module root directory.

Example 19.1 Source Code Files for the adviceApp Application

Click here to view code image

// File path: src/model/com/passion/model/AdviceModel.java                  (1)
package com.passion.model;
public class AdviceModel {
  private String currentAdvice;
  public AdviceModel() { this.setCurrentAdvice(0); }
  public void setCurrentAdvice(int i) {
    String advice;
    switch(i) {
      case 1 : advice = “See no evil.”; break;
      case 2 : advice = “Speak no evil.”; break;
      case 3 : advice = “Hear no evil.”; break;
      default: advice = “No advice.”;
    }
    currentAdvice = advice;
  }
  public String getCurrentAdvice() { return currentAdvice; }
}

Click here to view code image

// File path: src/view/com/passion/view/AdviceView.java                     (2)
package com.passion.view;
import com.passion.model.AdviceModel;             // From model module.
public class AdviceView {
  private AdviceModel model;
  public AdviceView(AdviceModel model) { this.model = model; }
  public void updateAdviceDisplay(){
    System.out.println(model.getCurrentAdvice());
  }
}

Click here to view code image

// File path: src/controller/com/passion/controller/AdviceController.java   (3)
package com.passion.controller;
import com.passion.model.AdviceModel;               // From model module.
import com.passion.view.AdviceView;                 // From view module.
public class AdviceController {
  private AdviceModel model;
  private AdviceView view;
  public AdviceController() {
    model = new AdviceModel();
    view = new AdviceView(model);                   // Inject the model.
  }
  public void showAdvice(int adviceNumber) {
    model.setCurrentAdvice(adviceNumber);
    view.updateAdviceDisplay();
  }
}

Click here to view code image

// File path: src/main/com/passion/main/Main.java                           (4)
package com.passion.main;
import com.passion.controller.AdviceController;    // From controller module.

public class Main {
  public static void main(String… args) {
    AdviceController controller = new AdviceController();
    controller.showAdvice(1);
    controller.showAdvice(2);
    controller.showAdvice(3);
    controller.showAdvice(0);
  }
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *