Updated 8 July 2018
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 :
1 2 3 4 5 |
public interface Observerable { void addObserver(Observer myObserver); void removeObserver(Observer myObserver); void notifyObservers(boolean change);// params need to change as per your Observer. } |
Observer interface code Example :
1 2 3 |
public interface Observer { void onChange(boolean change); // params need to change as per your use case. } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class ObserverApplication extends Application { private List<Observer> observerList; @Override public void onCreate() { super.onCreate(); observerList = new ArrayList<>(); // Other Code } public List<Observer> getObserverList() { return observerList; } } |
BaseActivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class BaseActivity extends AppCompatActivity implements Observerable{ // Other Code as per your requirement @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Other Code as per your requirement } @Override public void notifyObservers(boolean change) { for (Observer observer : ((ObserverApplication)BaseActivity.this.getApplication()).getObserverList()){ observer.onchange(change); } } @Override public void addObserver(Observer myObserver) { ((ObserverApplication)BaseActivity.this.getApplication()).getObserverList().add(myObserver); } @Override public void removeObserver(Observer myObserver) { ((ObserverApplication)BaseActivity.this.getApplication()).getObserverList().remove(myObserver); } } |
Main Activity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class MainActivity extends BaseActivity implements Observer{ // Other Code as per your requirement @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Other Code as per your requirement } @Override public void onChange(boolean change) { // implement your logic for change over here notifyObservers(boolean change)// Code to notify all the classes observing this instance } @Override protected void onStart() { super.onStart(); addObserver(this); } @Override protected void onStop() { super.onStop(); removeWishlistObserver(this); } } |
And with this, you have successfully implemented the Observer Design Pattern in your Android Application.
Keep coding and Keep Sharing 🙂
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.