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:





Thursday 11 January 2018

Facade Pattern Example in Android


Facade Design Pattern in Android


What is Facade Pattern?
Facade pattern describes a higher-level interface that makes sub-system easier to use.By using facade pattern hides all the complexities of the system and it creates an interface which one client can access. Basically we use facade pattern when you want to provide simple interface to a complex sub-system.


Please check-out the bellow flow-chart, we will implement facade based pattern by following this flowchart:




Now we will do some example, which will help you more to understand. 

Firstly we will create an interface name MobileDistributor.java
In this interface we will create two method, name mobileModelNo() and mobilePrice().

MobileDistributor.java

public interface MobileDistributor {

    
public void mobileModelNo();
    
public void mobilePrice();

}

Now we will create three concrete class name iPhone,Nexus and Sony. These class will implement MobileDistributor interface.

Creating iPhone class with implementation as follows:

public class iPhone implements MobileDistributor{
    
@Override
    
public void mobileModelNo() {
        System.
out.println("IphoneModel: iPhone X");
    }

    
@Override
    
public void mobilePrice() {

        System.
out.println("iPhone X Price Rs. 85000");
    }
}

Next we will create Nexus class with following implementation:

public class Nexus implements MobileDistributor{
    
@Override
    
public void mobileModelNo() {
        System.
out.println("Nexus Model: Nexus6");
    }

    
@Override
    
public void mobilePrice() {

        System.
out.println("Nexus6 Price Rs. 22,000");
    }
}

and the last one we will create Sony class with implementation as follows:

public class Sony implements MobileDistributor{
    
@Override
    
public void mobileModelNo() {
        System.
out.println("Sony Model: Sony Xpheria");
    }

    
@Override
    
public void mobilePrice() {

        System.
out.println("Sony Xpheria price Rs. 15,000");
    }
}


Now we create Seller class which will use MobileDistributor interface , by using this interface we will access mobile price and mobile model number. Class with implementation as follows:


public class Seller {

    
private MobileDistributor iPhone,nexus,sony;


    
public Seller(){
        
iPhone new iPhone();
        
nexus new Nexus();
        
sony new Sony();
    }

    
public void iPhoneMobileSale(){
        
iPhone.mobileModelNo();
        
iPhone.mobilePrice();
    }


    
public void nexusMobileSale(){
        
nexus.mobileModelNo();
        
nexus.mobilePrice();
    }

    
public void sonyMobileSale(){
        
sony.mobileModelNo();
        
sony.mobilePrice();
    }

}

Now inside main activity, we will create a button , on button click will access price of all three mobile models.

Seller s = new Seller();
//For iPhone sale
s.iPhoneMobileSale();
//For nexus sale
s.nexusMobileSale();
//For Sony sale
s.sonyMobileSale();

Output will be as follows:

IphoneModel: iPhone X
iPhone X Price Rs. 85000
Nexus Model: Nexus6
Nexus6 Price Rs. 22,000
Sony Model: Sony Xpheria

Sony Xpheria price Rs. 15,000