Android App Development
iOS 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 22.214.171.124 release ready!
Owner and Founder. Opencart
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)
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*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Webkul is a truly trusted and supported IT Company to develop business ideas for e-commerce. They provide professional support after deploying the solution to production and be responsible to act for fixing the reported issues or system errors. We highly encourage to deal with Webkul for business development.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.