Added second MVC example. This time the view is registered as observer to the model and gets update notifications that way.
This commit is contained in:
parent
da6d92d125
commit
cd32e385e2
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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<GiantModelObserver> 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));
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user