Skip to content

Commit cd32e38

Browse files
committed
Added second MVC example. This time the view is registered as observer to the model and gets update notifications that way.
1 parent da6d92d commit cd32e38

File tree

9 files changed

+253
-0
lines changed

9 files changed

+253
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.iluwatar.model.view.controller.with.observer;
2+
3+
/**
4+
*
5+
* In this second example the model-view relationship is different. This time we use the Observer pattern to notify
6+
* the {@link GiantView} each time the {@link GiantModel} is changed. This way the {@link GiantController} responsibilities
7+
* are narrowed and it only needs to modify the {@link GiantModel} according to the user input.
8+
*
9+
*/
10+
public class App {
11+
12+
/**
13+
* Program entry point
14+
* @param args command line args
15+
*/
16+
public static void main( String[] args ) {
17+
// create model, view and controller
18+
GiantModel giant = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED);
19+
GiantView view = new GiantView();
20+
GiantController controller = new GiantController(giant, view);
21+
// initial display
22+
controller.updateView();
23+
// controller receives some interactions that affect the giant
24+
// model modifications trigger the view rendering automatically
25+
controller.setHealth(Health.WOUNDED);
26+
controller.setNourishment(Nourishment.HUNGRY);
27+
controller.setFatigue(Fatigue.TIRED);
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.iluwatar.model.view.controller.with.observer;
2+
3+
/**
4+
*
5+
* Fatigue enumeration
6+
*
7+
*/
8+
public enum Fatigue {
9+
10+
ALERT("alert"), TIRED("tired"), SLEEPING("sleeping");
11+
12+
private String title;
13+
14+
Fatigue(String title) {
15+
this.title = title;
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return title;
21+
}
22+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.iluwatar.model.view.controller.with.observer;
2+
3+
/**
4+
*
5+
* GiantController updates the giant model.
6+
*
7+
*/
8+
public class GiantController {
9+
10+
private GiantModel giant;
11+
private GiantView view;
12+
13+
public GiantController(GiantModel giant, GiantView view) {
14+
this.giant = giant;
15+
this.view = view;
16+
this.giant.registerObserver(this.view);
17+
}
18+
19+
public Health getHealth() {
20+
return giant.getHealth();
21+
}
22+
23+
public void setHealth(Health health) {
24+
this.giant.setHealth(health);
25+
}
26+
27+
public Fatigue getFatigue() {
28+
return giant.getFatigue();
29+
}
30+
31+
public void setFatigue(Fatigue fatigue) {
32+
this.giant.setFatigue(fatigue);
33+
}
34+
35+
public Nourishment getNourishment() {
36+
return giant.getNourishment();
37+
}
38+
39+
public void setNourishment(Nourishment nourishment) {
40+
this.giant.setNourishment(nourishment);
41+
}
42+
43+
public void updateView() {
44+
this.view.displayGiant(giant);
45+
}
46+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.iluwatar.model.view.controller.with.observer;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
*
8+
* GiantModel contains the giant data.
9+
*
10+
*/
11+
public class GiantModel {
12+
13+
private Health health;
14+
private Fatigue fatigue;
15+
private Nourishment nourishment;
16+
private List<GiantModelObserver> observers = new ArrayList<>();
17+
18+
GiantModel(Health health, Fatigue fatigue, Nourishment nourishment) {
19+
this.health = health;
20+
this.fatigue = fatigue;
21+
this.nourishment = nourishment;
22+
}
23+
24+
public Health getHealth() {
25+
return health;
26+
}
27+
28+
public void setHealth(Health health) {
29+
this.health = health;
30+
notifyObservers();
31+
}
32+
33+
public Fatigue getFatigue() {
34+
return fatigue;
35+
}
36+
37+
public void setFatigue(Fatigue fatigue) {
38+
this.fatigue = fatigue;
39+
notifyObservers();
40+
}
41+
42+
public Nourishment getNourishment() {
43+
return nourishment;
44+
}
45+
46+
public void setNourishment(Nourishment nourishment) {
47+
this.nourishment = nourishment;
48+
notifyObservers();
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return String.format("The giant looks %s, %s and %s.", health, fatigue, nourishment);
54+
}
55+
56+
public void registerObserver(GiantModelObserver observer) {
57+
observers.add(observer);
58+
}
59+
60+
private void notifyObservers() {
61+
observers.stream().forEach((GiantModelObserver o) -> o.modelChanged(this));
62+
}
63+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iluwatar.model.view.controller.with.observer;
2+
3+
/**
4+
*
5+
* GiantModelObserver is the interface for delivering update notifications.
6+
*
7+
*/
8+
public interface GiantModelObserver {
9+
10+
void modelChanged(GiantModel model);
11+
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.iluwatar.model.view.controller.with.observer;
2+
3+
/**
4+
*
5+
* GiantView displays the giant
6+
*
7+
*/
8+
public class GiantView implements GiantModelObserver {
9+
10+
public void displayGiant(GiantModel giant) {
11+
System.out.println(giant);
12+
}
13+
14+
@Override
15+
public void modelChanged(GiantModel model) {
16+
displayGiant(model);
17+
}
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.iluwatar.model.view.controller.with.observer;
2+
3+
/**
4+
*
5+
* Health enumeration
6+
*
7+
*/
8+
public enum Health {
9+
10+
HEALTHY("healthy"), WOUNDED("wounded"), DEAD("dead");
11+
12+
private String title;
13+
14+
Health(String title) {
15+
this.title = title;
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return title;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.iluwatar.model.view.controller.with.observer;
2+
3+
/**
4+
*
5+
* Nourishment enumeration
6+
*
7+
*/
8+
public enum Nourishment {
9+
10+
SATURATED("saturated"), HUNGRY("hungry"), STARVING("starving");
11+
12+
private String title;
13+
14+
Nourishment(String title) {
15+
this.title = title;
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return title;
21+
}
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.iluwatar.model.view.controller.with.observer;
2+
3+
import org.junit.Test;
4+
5+
import com.iluwatar.model.view.controller.with.observer.App;
6+
7+
/**
8+
*
9+
* Application test
10+
*
11+
*/
12+
public class AppTest {
13+
14+
@Test
15+
public void test() {
16+
String[] args = {};
17+
App.main(args);
18+
}
19+
}

0 commit comments

Comments
 (0)