Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
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.
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).
The basic implementation of the Observer Pattern can be done in two ways in your android application.
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)
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 🙂
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
All communication is done through a ticket system. I appreciate the regular almost daily updates of what is being worked on, and communicating changes or updates is easy through this system. Mobikul's response timeframe is quick.
Josh Arnold
Owner, Shop ATV Escape
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.