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

No comments:

Post a Comment