13

I have to do a design of a DownloadManager, but my main question is related to the notifications that a Download can send to the DownloadManager like onUpdate() to update a progress bar, onError(), onFinish(), etc. Somehow the DownloadManager has to receive this notifications from its Downloads.

I've thought 2 possible ways:

  • Observer pattern
  • Callbacks

Observer pattern

Basically there are 1 Observable and N Observers. In my case the DownloadManager has te be an Observer and the Downloads the Observables, so the relation is N Observables 1 Observer, just the opposite.

The advantage is to centralize all the possible notifications in one method, the notify() or update() (from java) method from the Observers, in my case only the DownloadManager. I can pass a param to the notify() method with the code of the notification.

Disadvantage? I'm using an oop pattern for a thing that can be done easily with a callback. Also, N observables 1 observer it's something weird, at least with the observer pattern because this pattern was done for 1 observable N observers, so I really won't be using the observer pattern.

Callback

Very similar to the observer pattern. The DownloadManager implements a "listener" (interface). This listener implements the notification functions onFinish(), onUpdate(), etc. Then this listener must be registered in all Downloads, so when a Download finishes it will call listener.onFinish(). Additionally I can pass parameters to this methods from the Downloads, like in the observer pattern.

Advantage: Easily usage. Disadvantage: None.

I will probably use a callback because in my opinion it makes no sense to use an observer pattern for 1 observer N observables.

And you, which option will use?

Gabriel Llamas
  • 18,244
  • 26
  • 87
  • 112
  • 1
    "Callback. Advantage: Easily usage. Disadvantage: None." I think you've found your answer. – Szymon Rozga Jan 05 '11 at 15:11
  • 1
    One disadvantage of using callbacks is that they increase the coupling of the program (as they typically add a new parameter to all functions that use them). – synack Mar 24 '15 at 21:23
  • I think Callback what you have explained here again a Observer design pattern with multiple (something like Overloaded) update(Observable o, Object arg) methods for convenience of different status change updates. – Kanagavelu Sugumar May 29 '16 at 14:55

4 Answers4

3

Callbacks FTW. It is simpler, and in the vast majority of cases, simplicity influences every other aspect of the project in a positive way, including development, debugging, optimization, documentation and further maintenance.

Caleb Hattingh
  • 9,005
  • 2
  • 31
  • 44
  • 5
    5 years later... The "right way" seems to be the Observer pattern. Look at most JavaScript APIs : they are replacing the callbacks by Promises. Look at Angular 2 : they have replaced the callbacks and Promises by Observables. So callbacks are easy, but not a good choice for the long term. – CedX Nov 16 '16 at 12:31
3

The observer is more flexible/scalable. Is not that weird after all the usage you mentioned for the observer pattern. Patters are after all just guidlines, if you need to change it a bit to fit your needs then go for it.

Consider the case when you have multiple DownloadManagers (maybe this is not a valid use case for your particular situation). You will need to register both of them. If you have even more then you will have to register all of them. Pretty long list, plus you need to implement listeners management in the Downloads

Liviu Mandras
  • 6,540
  • 2
  • 41
  • 65
3

There is also one option, which is opposite to Observer/Callback approach. You can turn clients of DownloadManager from passive to active players of the show. Instead of waiting for a message from the manager they will regularly request its status.

This way your download process will look much smoother to the end-user. And you will be able to control it better.

Of course, you will have to use two threads. Or you will have to teach the DownloadManager to work in small steps, returning control to its client, instead of running one unbreakable piece of work.

yegor256
  • 102,010
  • 123
  • 446
  • 597
3

observer is more suitable because Manager need to send request to many instances.

It is easy to implement. In implementation point of it will be more scalable in terms of progress bar functionality and how much downloaded each one and Total % Download

Kamahire
  • 2,149
  • 3
  • 21
  • 50