Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observer pattern or Callback?

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?

like image 434
Gabriel Llamas Avatar asked Jan 03 '11 09:01

Gabriel Llamas


People also ask

What is callback in Observer pattern?

callback - notifies a single caller that some operation finished with some result. observer - notifies zero to n interested parties that some event (for example a finished operation) happened.

What is difference between Observer and callback in rails?

Usually, you can only pass a single callback.. Example: Running a thread and giving a callback that is called when the thread terminates. An observer lives longer and it can be attached/detached at any time. There can be many observers for the same thing and they can have different lifetimes.

When should Observer Pattern be used?

Observer Pattern is one of the behavioral design pattern. Observer design pattern is useful when you are interested in the state of an object and want to get notified whenever there is any change.

What is callback pattern?

Wikipedia says. In computer programming, a callback, also known as a "call-after" function, is any executable code that is passed as an argument to other code; that other code is expected to call back (execute) the argument at a given time. Programmatic Example. Callback is a simple interface with single method.


1 Answers

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.

like image 62
yegor256 Avatar answered Oct 01 '22 04:10

yegor256