Here we see one-to-many relationship. For example, an event of "get hit" can cause energy, power, speed, looking and other conditions to change; energy condition can be changed by events such as "get hit", "get inspired", "get rest". The thing that varies are the state of the events and the number and type of the conditions that get effected by a specific event. The thing that stay constant is a subject-observer relationship in the picture, therefore we abstract them into two interfaces: Observable and Observer.
Show   Hide
Here is the text you would like to show
/*EventsMonitor.java*/ import java.util.Observable; import java.util.Observer; public class EventsMonitor extends Observable{ private float power; private float energy; private float speed; public EventsMonitor(){}; public void ConditionChanged(){ super.setChanged(); super.notifyObservers(); } public void setConditions(float power, float energy, float speed){ this.power = power; this.energy = energy; this.speed = speed; } public void Hit() { this.power --; this.energy --; this.speed --; } public void Inspired(){ this.power++; this.speed++; } public void Rested(){ this.energy++; this.power++; this.speed++; } public float getPower(){ return this.power; } public float getEnergy(){ return this.energy; } public float getSpeed(){ return this.speed; } } |
/*EnergyDisplay.java*/ import java.util.Observable; import java.util.Observer; public class EnergyDisplay implements Observer{ Observable observerable; private float energy; public EnergyDisplay(Observable o){ this.observerable = o; observerable.addObserver(this); } public void update(Observable obs, Object arg){ if(obs instanceof EventsMonitor){ EventsMonitor event = (EventsMonitor)obs; this.energy = event.getEnergy(); display(); } } public void display(){ System.out.println("The Energy changed! Energy is "+this.energy); } } |
/*PowerDisplay.java*/ import java.util.Observable; import java.util.Observer; public class PowerDisplay implements Observer{ Observable observerable; private float power; public PowerDisplay(Observable o){ this.observerable = o; observerable.addObserver(this); } public void update(Observable obs, Object arg){ if(obs instanceof EventsMonitor){ EventsMonitor event = (EventsMonitor)obs; this.power = event.getPower(); display(); } } public void display(){ System.out.println("The power changed! power is "+this.power); } } |
/*SpeedDisplay.java*/ import java.util.Observable; import java.util.Observer; public class SpeedDisplay implements Observer{ Observable observerable; private float speed; public SpeedDisplay(Observable o){ this.observerable = o; observerable.addObserver(this); } public void update(Observable obs, Object arg){ if(obs instanceof EventsMonitor){ EventsMonitor event = (EventsMonitor)obs; this.speed = event.getSpeed(); display(); } } public void display(){ System.out.println("The Speed changed! Speed is "+this.speed); } } |
/*HealthMonitor.java*/ public class HealthMonitor { public static void main(String[] args) { EventsMonitor event = new EventsMonitor(); PowerDisplay power = new PowerDisplay(event); EnergyDisplay energy = new EnergyDisplay(event); SpeedDisplay speed = new SpeedDisplay(event); event.setConditions(10.0f, 10.0f, 10.0f); event.ConditionChanged(); event.Hit(); event.ConditionChanged(); event.Hit(); event.ConditionChanged(); event.Inspired(); event.ConditionChanged(); } } |
No comments:
Post a Comment