3

Aren't these competitors? I'm thinkin they're not, but don't see it.

How about within the context of an Activity needing to learn when a Service has new xyz?

Thanks!

DJC
  • 3,243
  • 3
  • 27
  • 31
  • By competitors you mean two different ways to accomplish the same thing? They do have slightly different purposes.. the looper is to help you create a message loop, the observer is to let you get state updates on an object you are observing. Its really not clear what your question is though.. – Cheryl Simon Nov 01 '10 at 23:18
  • And the purpose of a Looper is to handle an asynchronous Message ... just as an Observer handles an asynchronous Observable. My question is, why use a looper/handler? – DJC Nov 02 '10 at 00:09
  • 1
    Observer callbacks aren't necessarily asynchronous, it's just a way of decoupling components. With a handler, messages are processed on the current thread at some point in the future. – dhaag23 Nov 02 '10 at 00:16

1 Answers1

1

They are for different purpose so you can't compare in the way that one exclude the other as perhaps you may intend. I explain:

  • Registered Observers receive notification of a change all together sequentially simply calling once notifyObservers(..).

  • Handlers allow you to modify UI components from a background thread but you handle/update only 1 "observer" (the one handled by the Handler).

More advanced, if you think, you can even combine the two, to always be exception free while update UI from a background thread still keeping the Observer pattern.

I think nobody answered you in these 5 years because almost nobody is aware of great power of Observer pattern ;-)

Davideas
  • 3,226
  • 2
  • 33
  • 51
  • Could you elaborate on how these two patterns can be combined? For example, if I have a UI, an Observer for this UI, a Handler that process some request initiated from this UI, and a Network component that get the actual response to this request. What is the flow if I want to combine these two patterns? Thanks! – jiu9x9uij Jul 24 '15 at 03:02