Monday, 15 January 2018

Observer Pattern Example in Android

Observer Design Pattern in Android


In previous tutorial we saw Facade pattern,next we will look into observer pattern.

Observer pattern is a behavioral design pattern. If the state of one of the object changes, all of the other objects that depend on object are get notified. It’s like one to many dependency between objects, so when an objects changes it’s state, related to objects that depends upon also get notified and updated automatically.  

Subject contains list of observers, it basically helps observers to notify when any change of it’s state. Observer will be having a method to set the object to watch and another method that will be used by subject to notify them of any updates. 

Bellow see the diagram, we will follow this diagram for example,so it will be better to understand.




Now we will do some example, it will be very easy to understand.
Firstly we are creating an interface MyObserver with a method update().

MyObserver.java as follows

public interface MyObserver {

    
public void update(String currentEdition);

}

Next we will create concrete class Subscriber1 which will implement MyObserver interface.

Subscriber1.java is as follows

public class Subscriber1 implements MyObserver{
    
@Override
    
public void update(String currentEdition) {
        System.
out.println("Update from subscriber1");
    }
}

Next we will create another concrete class Subscriber2 which will implement MyObserver interface.

public class Subscriber2 implements MyObserver{
    
@Override
    
public void update(String currentEdition) {
        System.
out.println("Update from subscriber2");
    }
}

Next we will create MySubject interface, which basically use to register and un-register observers and also notify observers.

MySubject.java as follows:

public interface MySubject {

    
public void registerObservers(MyObserver registerObserver);
    
public void unRegisterObservers(MyObserver unregisterObserver);
    
public void notifyObservers();

}

Next we will create Publisher class which basically implements MySubject interface.

Publisher.java class is as follows:

public class Publisher implements MySubject{

    List<MyObserver> 
listObserver new ArrayList<>();


    
@Override
    
public void registerObservers(MyObserver registerObserver) {
        System.
out.println("Registered observer");
        
listObserver.add(registerObserver);
    }

    
@Override
    
public void unRegisterObservers(MyObserver unregisterObserver) {
        System.
out.println("Unregister observer");
    }

    
@Override
    
public void notifyObservers() {

        
for(MyObserver obser : listObserver){
            obser.update(
"Today's Edition");
        }

    }
}
Next we will create an activity, inside this activity, just we will take a button and set a click listener on that. When you will click on that button, it will register observers and notify all. 

Inside your button click, just write-down the bellow code:

//Creating instance
MySubject publisher = new Publisher();

//Registering observers
MyObserver ob1 = new Subscriber1();
publisher.registerObservers(ob1);

MyObserver ob2 = 
new Subscriber2();
publisher.registerObservers(ob2);

//Notify observers
publisher.notifyObservers();

//Assume I am removing subscriber1
publisher.unRegisterObservers(ob1);

//Then again notify observers
publisher.notifyObservers();

Now run your app and check in logcat.
You will see bellow results:





No comments:

Post a Comment