Category: Blog, Android, Development, Java

Design Patterns in Android – Observer

Design Patters In Android Observer

The Observer pattern is one of the Behavioral Design Patterns, which means it enables communication between different classes and objects. Here, you can learn how to implement it step-by-step.


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. In my previous post, I introduced the Builder pattern. Now, it’s time to meet the Observer!

Introduction

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.

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 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.

The Observer pattern is also a great solution for designing your application to follow OCP (the open/closed principle), one of the SOLID rules which say that your application should be open for extension, yet closed for modification.

Observer – example

In this article, I’d like to introduce the Observer via a real example. Let’s say we have some Button, which is checkable. Every time someone clicks on it, it changes states between ON and OFF. In the Observer pattern, we’ll call this Button Subject.

We also have some objects which are interested in the Button’s state, e.g. every time the Button is clicked, we’d like to show a different message in every visible Fragment. Every Fragment interested in Button’s state, here in this pattern, will be called Observer.

Observer patterns give us the possibility to unsubscribe from observing the Subject at every moment, e.g. when we don’t want to show a message, such as when a Fragment is not visible.
To sum up, we have three fragments – Fragments A and B (our Observers) are interested in the Button’s state. The Button itself is located in Fragment C (it’s our Subject). Everything is placed in the Activity. Now, let’s start!

NEED A SUCCESSFUL TEAM?

We’re 100% office based team with 7-years’ experience
in mobile & web app development

Estimate project

Implementation

How can we implement the Observer pattern? We need to create interfaces for Subject and Observer. Our Subject needs to handle subscribing, unsubscribing and the updating of data:

And there’s complementary Observer, with the main function of receiving info about updates from Subject:

Now we need to implement those interfaces in the proper places. Our Subject is Fragment C (ThirdFragment), because it has a Button:

Fragment C implements three methods – for registering, unregistering, and notifying observers. Thanks to these methods, we’ll store our observers in the list and, if there’s a need to notify them about some action, we can iterate through the list and run the proper callbacks.

We can also see the listener method set to the Button. Every time we click on the Button, we call notifyObservers(), so they all know about the event.

Now we can go to the Fragments A and B. They are both our Observers, so they need to implement one method:

Whenever some action occurs, we’ll be notified about this in this method, so we can show the proper UI, for example. Notice that the boolean value is checked — it’s the value passed from the Button. To make it all work, we need to tell our Fragment C (Subscriber) to register both Fragment A and B (Observers). We can use the main activity to do this:

From now on, Fragments A and B are registered to Fragment C and they will be updated about the state from Fragment C. The last thing we need to remember about is unregistering from observing this fragment, to avoid memory leaks. And that’s all!

Conclusion

The Observer pattern gives us loose coupling between single places in our application (e.g. between many fragments). Using this pattern, a subject can register an unlimited number of observers, interested on watching its state, without them knowing about each other. It’s a great solution for implementing communication in modules with one-to-many relationships.

About the author

Paulina Szklarska

Paulina Szklarska

Android Developer

Android Developer @ DroidsOnRoids