Skip to content

Commit 3bcca71

Browse files
committed
Completed MVC example code.
1 parent c9bf681 commit 3bcca71

File tree

7 files changed

+154
-1
lines changed

7 files changed

+154
-1
lines changed

model-view-controller/src/main/java/com/iluwatar/App.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
public class App {
44

55
public static void main( String[] args ) {
6-
System.out.println( "Hello World!" );
6+
// create model, view and controller
7+
GiantModel giant = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED);
8+
GiantView view = new GiantView();
9+
GiantController controller = new GiantController(giant, view);
10+
// initial display
11+
controller.updateView();
12+
// controller receives some interactions that affect the giant
13+
controller.setHealth(Health.WOUNDED);
14+
controller.setNourishment(Nourishment.HUNGRY);
15+
controller.setFatigue(Fatigue.TIRED);
16+
// redisplay
17+
controller.updateView();
718
}
819
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.iluwatar;
2+
3+
public enum Fatigue {
4+
5+
ALERT("alert"), TIRED("tired"), SLEEPING("sleeping");
6+
7+
private String title;
8+
9+
Fatigue(String title) {
10+
this.title = title;
11+
}
12+
13+
@Override
14+
public String toString() {
15+
return title;
16+
}
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.iluwatar;
2+
3+
public class GiantController {
4+
5+
private GiantModel giant;
6+
private GiantView view;
7+
8+
public GiantController(GiantModel giant, GiantView view) {
9+
this.giant = giant;
10+
this.view = view;
11+
}
12+
13+
public Health getHealth() {
14+
return giant.getHealth();
15+
}
16+
17+
public void setHealth(Health health) {
18+
this.giant.setHealth(health);
19+
}
20+
21+
public Fatigue getFatigue() {
22+
return giant.getFatigue();
23+
}
24+
25+
public void setFatigue(Fatigue fatigue) {
26+
this.giant.setFatigue(fatigue);
27+
}
28+
29+
public Nourishment getNourishment() {
30+
return giant.getNourishment();
31+
}
32+
33+
public void setNourishment(Nourishment nourishment) {
34+
this.giant.setNourishment(nourishment);
35+
}
36+
37+
public void updateView() {
38+
this.view.displayGiant(giant);
39+
}
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.iluwatar;
2+
3+
public class GiantModel {
4+
5+
private Health health;
6+
private Fatigue fatigue;
7+
private Nourishment nourishment;
8+
9+
GiantModel(Health health, Fatigue fatigue, Nourishment nourishment) {
10+
this.health = health;
11+
this.fatigue = fatigue;
12+
this.nourishment = nourishment;
13+
}
14+
15+
public Health getHealth() {
16+
return health;
17+
}
18+
19+
public void setHealth(Health health) {
20+
this.health = health;
21+
}
22+
23+
public Fatigue getFatigue() {
24+
return fatigue;
25+
}
26+
27+
public void setFatigue(Fatigue fatigue) {
28+
this.fatigue = fatigue;
29+
}
30+
31+
public Nourishment getNourishment() {
32+
return nourishment;
33+
}
34+
35+
public void setNourishment(Nourishment nourishment) {
36+
this.nourishment = nourishment;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return String.format("The giant looks %s, %s and %s.", health, fatigue, nourishment);
42+
}
43+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.iluwatar;
2+
3+
public class GiantView {
4+
5+
public void displayGiant(GiantModel giant) {
6+
System.out.println(giant);
7+
}
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.iluwatar;
2+
3+
public enum Health {
4+
5+
HEALTHY("healthy"), WOUNDED("wounded"), DEAD("dead");
6+
7+
private String title;
8+
9+
Health(String title) {
10+
this.title = title;
11+
}
12+
13+
@Override
14+
public String toString() {
15+
return title;
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.iluwatar;
2+
3+
public enum Nourishment {
4+
5+
SATURATED("saturated"), HUNGRY("hungry"), STARVING("starving");
6+
7+
private String title;
8+
9+
Nourishment(String title) {
10+
this.title = title;
11+
}
12+
13+
@Override
14+
public String toString() {
15+
return title;
16+
}
17+
}

0 commit comments

Comments
 (0)