#108 Consistent package naming throughout the examples
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package com.iluwatar.model.view.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Model-View-Controller is a pattern for implementing user interfaces. It divides the application
|
||||
* into three interconnected parts namely the model, the view and the controller.
|
||||
*
|
||||
* The central component of MVC, the model, captures the behavior of the application in terms of its problem
|
||||
* domain, independent of the user interface. The model directly manages the data, logic and rules of the
|
||||
* application. A view can be any output representation of information, such as a chart or a diagram
|
||||
* The third part, the controller, accepts input and converts it to commands for the model or view.
|
||||
*
|
||||
* In this example we have a giant (GiantModel) with statuses for health, fatigue and nourishment. GiantView
|
||||
* can display the giant with its current status. GiantController receives input affecting the model and
|
||||
* delegates redrawing the giant to the view.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main( String[] args ) {
|
||||
// create model, view and controller
|
||||
GiantModel giant = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED);
|
||||
GiantView view = new GiantView();
|
||||
GiantController controller = new GiantController(giant, view);
|
||||
// initial display
|
||||
controller.updateView();
|
||||
// controller receives some interactions that affect the giant
|
||||
controller.setHealth(Health.WOUNDED);
|
||||
controller.setNourishment(Nourishment.HUNGRY);
|
||||
controller.setFatigue(Fatigue.TIRED);
|
||||
// redisplay
|
||||
controller.updateView();
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
package com.iluwatar.model.view.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Fatigue enumeration
|
||||
*
|
||||
*/
|
||||
public enum Fatigue {
|
||||
|
||||
ALERT("alert"), TIRED("tired"), SLEEPING("sleeping");
|
||||
|
||||
private String title;
|
||||
|
||||
Fatigue(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
package com.iluwatar.model.view.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* GiantController can update the giant data and redraw it using the view.
|
||||
*
|
||||
*/
|
||||
public class GiantController {
|
||||
|
||||
private GiantModel giant;
|
||||
private GiantView view;
|
||||
|
||||
public GiantController(GiantModel giant, GiantView view) {
|
||||
this.giant = giant;
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
public Health getHealth() {
|
||||
return giant.getHealth();
|
||||
}
|
||||
|
||||
public void setHealth(Health health) {
|
||||
this.giant.setHealth(health);
|
||||
}
|
||||
|
||||
public Fatigue getFatigue() {
|
||||
return giant.getFatigue();
|
||||
}
|
||||
|
||||
public void setFatigue(Fatigue fatigue) {
|
||||
this.giant.setFatigue(fatigue);
|
||||
}
|
||||
|
||||
public Nourishment getNourishment() {
|
||||
return giant.getNourishment();
|
||||
}
|
||||
|
||||
public void setNourishment(Nourishment nourishment) {
|
||||
this.giant.setNourishment(nourishment);
|
||||
}
|
||||
|
||||
public void updateView() {
|
||||
this.view.displayGiant(giant);
|
||||
}
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
package com.iluwatar.model.view.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* GiantModel contains the giant data
|
||||
*
|
||||
*/
|
||||
public class GiantModel {
|
||||
|
||||
private Health health;
|
||||
private Fatigue fatigue;
|
||||
private Nourishment nourishment;
|
||||
|
||||
GiantModel(Health health, Fatigue fatigue, Nourishment nourishment) {
|
||||
this.health = health;
|
||||
this.fatigue = fatigue;
|
||||
this.nourishment = nourishment;
|
||||
}
|
||||
|
||||
public Health getHealth() {
|
||||
return health;
|
||||
}
|
||||
|
||||
public void setHealth(Health health) {
|
||||
this.health = health;
|
||||
}
|
||||
|
||||
public Fatigue getFatigue() {
|
||||
return fatigue;
|
||||
}
|
||||
|
||||
public void setFatigue(Fatigue fatigue) {
|
||||
this.fatigue = fatigue;
|
||||
}
|
||||
|
||||
public Nourishment getNourishment() {
|
||||
return nourishment;
|
||||
}
|
||||
|
||||
public void setNourishment(Nourishment nourishment) {
|
||||
this.nourishment = nourishment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("The giant looks %s, %s and %s.", health, fatigue, nourishment);
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package com.iluwatar.model.view.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* GiantView displays the giant
|
||||
*
|
||||
*/
|
||||
public class GiantView {
|
||||
|
||||
public void displayGiant(GiantModel giant) {
|
||||
System.out.println(giant);
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
package com.iluwatar.model.view.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Health enumeration
|
||||
*
|
||||
*/
|
||||
public enum Health {
|
||||
|
||||
HEALTHY("healthy"), WOUNDED("wounded"), DEAD("dead");
|
||||
|
||||
private String title;
|
||||
|
||||
Health(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
package com.iluwatar.model.view.controller;
|
||||
|
||||
/**
|
||||
*
|
||||
* Nourishment enumeration
|
||||
*
|
||||
*/
|
||||
public enum Nourishment {
|
||||
|
||||
SATURATED("saturated"), HUNGRY("hungry"), STARVING("starving");
|
||||
|
||||
private String title;
|
||||
|
||||
Nourishment(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user