This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Is this a the correct way to implement the Observer-Observable pattern in Java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is this a the correct way to implement the Observer-Observable pattern in Java ?" Watch "Is this a the correct way to implement the Observer-Observable pattern in Java ?" New topic
Author

Is this a the correct way to implement the Observer-Observable pattern in Java ?

David Blaine
Ranch Hand

Joined: Mar 23, 2013
Posts: 70
I posted this question at stack overflow, but did not get answers for why my code may or may not need Observer observable pattern.
The code posted before the one below was causing a StackOverflow error. I fixed the error using the code below. If O-O is needed, then is the implementation
below correct ?

Problem at SO - webpage

To run my code, just copy and paste it into your IDE - everything is there. This is a flight monitoring program which uses the Observer-Observable design pattern. FlightStautsMonitor is the Observer which monitors the flight status (an integer) of a Flight. FlightStatusChangeEvent is a class which notes the change of status of a flight. Demo is the driver class, or class with main.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

David Blaine wrote:I posted this question at stack overflow, but did not get answers for why my code may or may not need Observer observable pattern.
The code posted before the one below was causing a StackOverflow error. I fixed the error using the code below. If O-O is needed, then is the implementation
below correct ?


I can't say if you should be using the Observable/Observer pattern, because it is not clear what you want to do. But if your intent is to use the Observable/Observer pattern - then no, you have not done it correctly. Here are some notes:

Why do you have two different methods for setting the status? One informs Observers, the other does not. This is a bad idea. The point of the Observable/Observer pattern is that the Observer should be informed whenever the Observable changes. By creating a (public none-the-less) method that short-circuits that is to break the entire implementation. It also confuses the API.

Get rid of the the setStatus() method.

One thing that happens enough for me to protect against it as a general rule is this: the Observer only cares when the Observable changes, and doesn't really care when the mutating method is called. So I usually check the value that the mutator changes and only notify Observers if it is different than the current value:

This also protects against possible stack overflow exceptions that may occur when the call tree gets too complex.



This is the part that is really confusing. The comment say 'keep calling this method every 10 sec to get latest flight status'. What that comment describes is called 'polling.' It is exactly what the Observable/Observer pattern is intended to get rid of. You should not need to poll if you are using Observable/Observer pattern, because at any given time you already know what the status is (the value last sent via the Observer). Rather than poll the status at a fixed time period, why not just call this method whenever a status is changed (i.e. the Observer is updated)?




This part is also very confusing. You have a list of Flights. The update method gives you the flight that has changed, and the new status. You then search through the list to find the same flight, then set its status (to the value it just informed you of).

First, you don't need to find the flight in your list. You are given the flight which changed as the parameter. Second, you do not need to set the flight's status - the flight just informed you of its status - it knows before you do.

So what do you want to do when the flight status changes? If it is 'make sure the flight status has changed' then forget that, you don't need to. Get rid of the wasted code, and don't implement the Observer/Observable pattern. If you don't know what you want to do when the flight status has changed, then again, get rid of the Observer/Observable because they aren't needed. If you have something that you want to do in response to the flight status change, then do whatever that in the update() method. But don't just re-set the value that you are being informed has been set.

Steve
David Blaine
Ranch Hand

Joined: Mar 23, 2013
Posts: 70
+1 million for the reply. It is nice and comprehensive. Guess what ? I had actually run into a stackoverflow while working on this code. The related SO post is here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is this a the correct way to implement the Observer-Observable pattern in Java ?
 
Similar Threads
Constructor
Is this MVC
problem in checking net connection
"Static reference to non-static method"
Arraylist of variable size in loop?