Observer Design Pattern using Java Android

The Observer Pattern is a software design pattern that establishes a one-to-many dependency between objects. Anytime the state of one of the objects (the “subject” or “observable”) changes, all of the other objects (“observers”) that depend on it are notified. Before starting this Observer Design Pattern, I would request the readers to please go through the basis understanding of Design Patterns Introduction and categorisations.

Table of Content

  1. What is this design pattern?
  2. Pros and cons
  3. Some real time examples
  4. UML diagrams
  5. Scenario for Observer Pattern Usage:
  6. How to relate with Android:
    Examples:
    • Click Listeners
    • Broadcast receiver
    • Handler passing technique
    • Interface passing technique
  7. How to relate with backend development:
  8. Any new libraries:
    • Asyn task
    • Adapter – adapter.notifyDataSetChange()
    • RxJava
    • Observer and observables
    • Event Bus
    • Otto

What is Observer design pattern?

If the state change of the object happened then all of its dependents are notified and updated automatically.

Subject or Observable: The object which is being watched

Observers or listeners: The objects which are watching the state changes.

Lifesaver in async calls.

Observer Pattern Pros & Cons

Pros:

  • Supports the principle to strive for loosley coupled designs between objects that interact.
  • Allows you to send data to many other objects in a very efficient mannor.
  • No modification is need to be done to the subject to add new observers.
  • You can add and remove observers at anytime.

Cons

  • Java’s built in class Observable forces the use of inheritance vs programming to an interface
  • Observable protects crucial methods which means you can’t even create an instance of the Observable class and compose it with your own objects, you have to subclass.
  • If not used carefully the observer pattern can add unecessary complexity
  • The order of Observer notifications is undependable

Some Real time example

Important Points on Observer Design Pattern

  • Subject provides interface for observers to register and unregister themselves with the subject.
  • Subject knows who its subscribers are.
  • Multiple observers can subscribe for notifications.
  • Subject publishes the notifications.
  • Subject just sends the notification saying the state has changed. It does not pass any state information.
  • Once the notification is received from subject, observers call the subject and get data that is changed.

UML Diagram

Scenario for Observer Pattern Usage

  • When multiple objects depend on state of one object and all these multiple objects should be in sync with the state of this one object then observer design pattern is the right choice to apply.
  • Consider an excel sheet, data is shown to the user in different views. Generally data is is shown in grid cells and as required different graphs, charts can be created for same data. Underlying data is same and when that data (subject) state changes all the different view are updated.

What is Observer and Observable and when should we use them?

I found some points from different resources:

  1. Observable is a class and Observer is an interface.
  2. The Observable class maintains a list of Observers.
  3. When an Observable object is updated, it invokes the update() method of each of its Observers to notify that, it is changed.

More programs using Java: Callback Mechanism

  1. With One Callback – No parameter. 
  2. With Two Callbacks – No parameter.
  3. With Two parameterized Callbacks.

2 Comments

  1. Pingback: design pattern | java | android - Technical Jungle

  2. Pingback: ContentObserver

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.