The observer pattern in Java. Is an observer watching different, unrelated types or classes?

Are objects of the same type observed in the observer’s pattern in the observer’s pattern? Is everything all right if ONE Observer watches different objects of different types of classes that are completely unrelated?

Example - Observer is CarDashBoard and Observable (s) - FuelTank, Speedometer, EngineThermometer, CarBattery, etc. Observed parameters are FuelLevel, speed, temperatureOfEngine, powerLevel respectively.


If I observe several unrelated types, then I will have to use the instanceof () method to check the observable class that notified the Observer. However, this seems like a bad approach at this link - http://www.javapractices.com/topic/TopicAction.do?Id=31

Another link related to instanceof is http://blog.joda.org/2007/01/java-language-dynamic-instanceof_7967.html

So, I thought that instead I would use the getClass () method, and then decide which action to perform according to the passed Observable. Is it possible to use getClass () for this purpose?

Is there any other alternative?

+4
source share
2 answers

The reason not to use instanceof is that it breaks into polymorphism. It will work, but it’s better to come up with a more object-oriented solution. Using getClass has the same problem.

I think I would create several observers, one for FuelTank, Speedometer, etc., and update them on CarDashboard. Something like that.

 public class CarDashboard { public int currentSpeed; public int fuelPercentage; //etc... } public class FuelObserver extends Observer { private CarDashboard dashboard; public FuelObserver(CarDashboard dashboard) { this.dashboard = dashboard; } public void update(Observable fuelTank, Object fuelLevel) { this.dashboard.fuelPercentage = (int)fuelLevel; } } //etc.. 
+2
source

You can use polymorphism. For example, let's say you have an abstract class that extends Observable :

 import java.util.Observable; public abstract class DashboardDataSource extends Observable { public abstract int getLevel(); } 

Then you have two classes that inherit from DashboardDataSource (in fact, you have as many as you need, I just use two as an example):

 public class FuelLevel extends DashboardDataSource { public void saySomething(){ setChanged(); notifyObservers(); } @Override public int getLevel() { // TODO Auto-generated method stub return 50; } } 

AND

 public class BatteryLevel extends DashboardDataSource { public void saySomething(){ setChanged(); notifyObservers(); } @Override public int getLevel() { // TODO Auto-generated method stub return 20; } } 

Then you can have a Dashboard like this:

 public class Dashboard implements Observer { @Override public void update(Observable o, Object arg) { DashboardDataSource d = (DashboardDataSource) o; System.out.println (d.getLevel()); } } 

In this case, you simply throw the Observable o on the DashboardDataSource and call the implementation of its abstract method, which will be specific to anyone that DashboardDataSource makes a notification for your Dashboard (in this example, a FuelLevel or BatteryLevel )

+3
source

All Articles