I have asked this question before at stack overflow.
I need to know if combining observer pattern with timer task is okay or not. If not, will it lead to any serious problems ?
In a previous post, I used the observer pattern. Now, I want to do more with that code.
class Flight has a status (ie int) - before time, on time, late. This is my Observable class FlightStatusMonitor has an ArrayList of Flights. This class is my observer. There is only one such observer. The update(Observable o, Object arg) method will
update the status of the flight and also display the refreshed flight status of all flights that it observes.
I have a set of flights whose status I will change in batches. Batch size can be 1 flight or more - 1 , 5 , 15 ,22 , 45 etc BUT NEVER all flights.
I change the status for one batch, a couple of seconds later I change the status for another batch etc. Some flights remain unchanged.
I was thinking of using timer tasks to change the status of flights at chosen times and then see the updated status for all flights.
I want to be able to see the flight status displayed on the screen just after it is changed by a timer task.
But, I am not sure if I am doing this correctly. Will concurrency will be a problem here ?
It ishard to say if you are doing it right if we can't see what you are doing. Is it bad form to use a Timer Task to modify an Observable and have an observer display the current status? No, the is nothing wrong with that conceptually. But I think we would need to see what you are doing to make Any more constructive comments on it.
Joined: Mar 23, 2013
Steve Luke wrote:It ishard to say if you are doing it right if we can't see what you are doing. Is it bad form to use a Timer Task to modify an Observable and have an observer display the current status? No, the is nothing wrong with that conceptually. But I think we would need to see what you are doing to make Any more constructive comments on it.
What kind of details would enable you to help me ? Can you name them ? For example - give me all your code or show me your class design etc.
David Blaine wrote:What kind of details would enable you to help me ? Can you name them ? For example - give me all your code or show me your class design etc.
I suggest you look at the HowToAskQuestionsOnJavaRanch (←click) page; it has all sorts of useful information. In particular, read the two links at the bottom of the page (especially the first one). You might also want to read up about SSCCE's.
What we absolutely don't want is a mind-dump; so if you post code, be judicial about it.
The only thing I can add to Steve's advice is that, although there's nothing particularly wrong about mixing Observer/Observable with a timer, it seems more likely that you should keep them separate.
Observer/Observable is a subscription/notification system that normally operates in real time - Observable gets updated or receives new information, and notifies its subcribers. So, in your case, your Batchmight be an Observable, but I don't see any particular reason why you'd need to apply the timer directly to the observation mechanism; just apply it to the Batch.
What seems more likely to me is that you'd use the Batch to rationalize your updates - ie, if the same Batch contains several updates for the same object, group them together into a single transaction.
But it's just a thought; and without more information, its difficult to say anything with certainty.
Isn't it funny how there's always time and money enough to do it WRONG?