Completed MVC example code.
This commit is contained in:
parent
c9bf6819e1
commit
3bcca7102c
@ -3,6 +3,17 @@ package com.iluwatar;
|
||||
public class App {
|
||||
|
||||
public static void main( String[] args ) {
|
||||
System.out.println( "Hello World!" );
|
||||
// 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,17 @@
|
||||
package com.iluwatar;
|
||||
|
||||
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,40 @@
|
||||
package com.iluwatar;
|
||||
|
||||
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,43 @@
|
||||
package com.iluwatar;
|
||||
|
||||
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,8 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class GiantView {
|
||||
|
||||
public void displayGiant(GiantModel giant) {
|
||||
System.out.println(giant);
|
||||
}
|
||||
}
|
17
model-view-controller/src/main/java/com/iluwatar/Health.java
Normal file
17
model-view-controller/src/main/java/com/iluwatar/Health.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.iluwatar;
|
||||
|
||||
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,17 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public enum Nourishment {
|
||||
|
||||
SATURATED("saturated"), HUNGRY("hungry"), STARVING("starving");
|
||||
|
||||
private String title;
|
||||
|
||||
Nourishment(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user