Completed MVC example code.

This commit is contained in:
Ilkka Seppala 2015-05-02 23:37:26 +03:00
parent c9bf6819e1
commit 3bcca7102c
7 changed files with 154 additions and 1 deletions

View File

@ -3,6 +3,17 @@ package com.iluwatar;
public class App { public class App {
public static void main( String[] args ) { 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();
} }
} }

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,8 @@
package com.iluwatar;
public class GiantView {
public void displayGiant(GiantModel giant) {
System.out.println(giant);
}
}

View 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;
}
}

View File

@ -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;
}
}