diff --git a/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/App.java b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/App.java new file mode 100644 index 000000000..0b74f4b5a --- /dev/null +++ b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/App.java @@ -0,0 +1,29 @@ +package com.iluwatar.model.view.controller.with.observer; + +/** + * + * In this second example the model-view relationship is different. This time we use the Observer pattern to notify + * the {@link GiantView} each time the {@link GiantModel} is changed. This way the {@link GiantController} responsibilities + * are narrowed and it only needs to modify the {@link GiantModel} according to the user input. + * + */ +public class App { + + /** + * Program entry point + * @param args command line args + */ + 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 + // model modifications trigger the view rendering automatically + controller.setHealth(Health.WOUNDED); + controller.setNourishment(Nourishment.HUNGRY); + controller.setFatigue(Fatigue.TIRED); + } +} diff --git a/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/Fatigue.java b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/Fatigue.java new file mode 100644 index 000000000..301a579fb --- /dev/null +++ b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/Fatigue.java @@ -0,0 +1,22 @@ +package com.iluwatar.model.view.controller.with.observer; + +/** + * + * 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; + } +} diff --git a/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/GiantController.java b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/GiantController.java new file mode 100644 index 000000000..e892b2946 --- /dev/null +++ b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/GiantController.java @@ -0,0 +1,46 @@ +package com.iluwatar.model.view.controller.with.observer; + +/** + * + * GiantController updates the giant model. + * + */ +public class GiantController { + + private GiantModel giant; + private GiantView view; + + public GiantController(GiantModel giant, GiantView view) { + this.giant = giant; + this.view = view; + this.giant.registerObserver(this.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); + } +} diff --git a/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/GiantModel.java b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/GiantModel.java new file mode 100644 index 000000000..84b32a50a --- /dev/null +++ b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/GiantModel.java @@ -0,0 +1,63 @@ +package com.iluwatar.model.view.controller.with.observer; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * GiantModel contains the giant data. + * + */ +public class GiantModel { + + private Health health; + private Fatigue fatigue; + private Nourishment nourishment; + private List observers = new ArrayList<>(); + + 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; + notifyObservers(); + } + + public Fatigue getFatigue() { + return fatigue; + } + + public void setFatigue(Fatigue fatigue) { + this.fatigue = fatigue; + notifyObservers(); + } + + public Nourishment getNourishment() { + return nourishment; + } + + public void setNourishment(Nourishment nourishment) { + this.nourishment = nourishment; + notifyObservers(); + } + + @Override + public String toString() { + return String.format("The giant looks %s, %s and %s.", health, fatigue, nourishment); + } + + public void registerObserver(GiantModelObserver observer) { + observers.add(observer); + } + + private void notifyObservers() { + observers.stream().forEach((GiantModelObserver o) -> o.modelChanged(this)); + } +} diff --git a/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/GiantModelObserver.java b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/GiantModelObserver.java new file mode 100644 index 000000000..6363ef4f7 --- /dev/null +++ b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/GiantModelObserver.java @@ -0,0 +1,12 @@ +package com.iluwatar.model.view.controller.with.observer; + +/** + * + * GiantModelObserver is the interface for delivering update notifications. + * + */ +public interface GiantModelObserver { + + void modelChanged(GiantModel model); + +} diff --git a/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/GiantView.java b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/GiantView.java new file mode 100644 index 000000000..f4cebad80 --- /dev/null +++ b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/GiantView.java @@ -0,0 +1,18 @@ +package com.iluwatar.model.view.controller.with.observer; + +/** + * + * GiantView displays the giant + * + */ +public class GiantView implements GiantModelObserver { + + public void displayGiant(GiantModel giant) { + System.out.println(giant); + } + + @Override + public void modelChanged(GiantModel model) { + displayGiant(model); + } +} diff --git a/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/Health.java b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/Health.java new file mode 100644 index 000000000..e4b8ed9a7 --- /dev/null +++ b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/Health.java @@ -0,0 +1,22 @@ +package com.iluwatar.model.view.controller.with.observer; + +/** + * + * 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; + } +} diff --git a/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/Nourishment.java b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/Nourishment.java new file mode 100644 index 000000000..c1a8253c3 --- /dev/null +++ b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/with/observer/Nourishment.java @@ -0,0 +1,22 @@ +package com.iluwatar.model.view.controller.with.observer; + +/** + * + * 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; + } +} diff --git a/model-view-controller/src/test/java/com/iluwatar/model/view/controller/with/observer/AppTest.java b/model-view-controller/src/test/java/com/iluwatar/model/view/controller/with/observer/AppTest.java new file mode 100644 index 000000000..9a43ce7bc --- /dev/null +++ b/model-view-controller/src/test/java/com/iluwatar/model/view/controller/with/observer/AppTest.java @@ -0,0 +1,19 @@ +package com.iluwatar.model.view.controller.with.observer; + +import org.junit.Test; + +import com.iluwatar.model.view.controller.with.observer.App; + +/** + * + * Application test + * + */ +public class AppTest { + + @Test + public void test() { + String[] args = {}; + App.main(args); + } +}