Start a Project

Observer Design Pattern Android Implementation

In this blog, we will learn about implementing the Observer Design Pattern in Android Programming.

If you are using RxJava or RxAndroid, then you might be comfortable with the observer pattern and you can skip the basic introduction part.

Observer Design Pattern

Firstly, what is a “Design Pattern“?

Design patterns are reusable solutions to the most commonly occurring software problems. They can speed up the development process by providing a proven way of resolving frequent issues.

Design Patterns don’t provide you the code but they help you understand the solution to a known problem in theory.

With this in mind, you can write code efficiently.

The Observer pattern is one of the Behavioral Design Patterns, which means it gives us the way to communicate between different classes and objects. This pattern is the best approach if we have a one-to-many relation and we’d like to inform other objects about some changes or actions. For instance, you can notify all the classes accessing a change in the database object so that they can update.

You may know the Observer pattern from RxJava, where it’s implemented in a very extensive way. We have Observable, which is an object we’d like to observe (it may be also called Subject in the Observer pattern) and we can subscribe for observing changes in those object. Every object interested in those changes will be called the Observer. We can have one Subject and many Observers, and these single Observers may know nothing about each other. We can also stop observing changes at any time.

So, we conclude that :

1) Observable(Subject) is an object that actually changes or contains the actual change that happened

2) An observer is our class which is holding an instance of the data(Observable).

Implementation

The basic implementation of the Observer Pattern can be done in two ways in your android application.

  1. Using Broadcast Receivers: For this, you can see the official documents from here. But you will need to pass an intent Filter and send broadcasts for everytime any update occurs
  2. Using Your custom methods: We can see the detailed implementation below.

For implementing the Observer Design Pattern, we need to create two interfaces, one for notifying all the observers that data has changed and one for receiving the update of the actual change.

Along with this a list of what all classes actually need to be notified.

So our Observable(Subject) should actually implement the first interface and our observer should implement the second interface.

Observable Interface code example :

Observer interface code Example :

Now, in all the activities/fragments on which you want to notify the change, you will need to implement the Observable interface.

(I did it in my Base Activity)

And all the possible classes that can change your data you need to implement the Observer interface.

And in the application class or any base class, you will need to maintain a list of all the classes implementing the Observable interface.

(I did it in my application class)

Sample Code

Application class

BaseActivity

Main Activity

 

And with this, you have successfully implemented the Observer Design Pattern in your Android Application.

Keep coding and Keep Sharing 🙂

Exit mobile version