Using Observer Pattern In Android

This tutorial will guide you to use Observer pattern in Android. But before starting this you should need to know what is exactly Observer Pattern.

What Is Observer Pattern ?
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.

Let’s use the example of users that have subscribed to receive offers from Shopping site like Flipkart/Snapdeal via email. The users in this case are observers. Anytime there is an offer from Shopping site, they get notified about it via email. Each user can then either buy into the offer or decide that they might not be really interested in it at that moment. A user (an observer) can also subscribe to receive offers from another e-commerce marketplace if they want and might later completely unsubscribe from receiving offers from any of them.

This pattern is very similar to the Publish-Subscribe pattern. The subject or observable publishes out a notification to the dependent observers without even knowing how many observers have subscribed to it, or who they are—the observable only knows that they should implement an interface (we’ll get to that shortly), without worrying about what action the observers might perform.

Benefits of the Observer Pattern

  • The subject knows little about its observers. The only thing it knows is that the observers implement or agree to a certain contract or interface.

  • Subjects can be reused without involving their observers, and the same goes for observers too.

  • No modification is done to the subject to accommodate a new observer. The new observer just needs to implement an interface that the subject is aware of and then register to the subject.

  • An observer can be registered to more than one subject it’s registered to.

All these benefits give you loose coupling between modules in your code, which enables you to build a flexible design for your application. In the rest of this post, we’ll look at how to create our own Observer pattern implementation, and we’ll also use the built-in Java Observer/Observable API as well as looking into third-party libraries that can offer such functionality.

Utilising Java’s Built-In Observer API

Java has built-in Observer / Observable support in its API. In this section, we are going to use this. This API simplifies some of the implementation, as you’ll see.

1. Create the Observable

Our UserDataRepository which is our subject or observable will now extend the java.util.Observable superclass to become an Observable. This is a class that wants to be observed by one or more observers.

The first thing to notice is that we are extending a super class (this means that this class can’t extend any other class)

Registration, removal, or notification of observers—java.util.Observable is handling all of those for us.

We are employing a pull style. We alert the observers that a change has happened with notifyObservers(), but the observers will need to pull the data using the field getters we have defined in this class. If you want to use the push style instead, then you can use the method notifyObservers(Object arg) and pass the changed data to the observers in the object argument.

The setChanged() method of the super class sets a flag to true, indicating that the data has changed. Then you can call the notifyObservers() method. Be aware that if you don’t call setChanged() before calling notifyObsevers(), the observers won’t be notified. You can check the value of this flag by using the method hasChanged() and clear it back to false with clearChanged(). Now that we have our observable class created, let’s see how to set up an observer also.

2. Create the Observer

Our UserRepository observable class needs a corresponding Observer to be useful, so let’s create our UserProfileActivity to implement the java.util.Observer interface.

In the onCreate() method, we add this class as an observer to the UserRepository observable by using the addObserver() method in the java.util.Observable super class.

In the update() method which the observer must implement, we check if the Observable we receive as a parameter is an instance of our UserRepository (note that an observer can subscribe to different observables), and then we cast it to that instance and retrieve the values we want using the field getters. Then we use those values to update the view widgets.

When the activity is destroyed, we don’t need to get any updates from the observable, so we’ll just remove the activity from the observer list by calling the method deleteObserver().

Thanks for reading

***You can download complete code for Android Studio from Github***
https://github.com/codezlab/ObserverPatternsExample

Categories: Android,Observer Patterns

Leave A Reply

Your email address will not be published.