Sunday, 16 February 2020

Facade Pattern


Facade Pattern Intent

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
Wrap a complicated subsystem with a simpler interface.
Main Intension to reduce Remote service calls.

UseCase:  
a) System - Udupi -  VegService
b) Sysem – Krutunga - Nonveg Service
Client using System service to order items.   
Udupi - System    
public class VegService {
           
            public String getDosha() {
                        System.out.println("Dosha");
                        return "Dosha";
            }
           
            public String getIdly() {
                        System.out.println("Idly");
                        return "Idly";
            }
           
            public String getPoori() {
                        System.out.println("Poori");
                        return "Poori";
            }
           
            public String getChapathi() {
                        System.out.println("Chapathi");
                        return "Chapathi";
            }
}

Krutunga - System
public class NonvegService {
           
            public String getChickenBiryani() {
                        System.out.println("Chicken Biryani");
                        return "ChickenBiryani";
            }
           
            public String getMuttonBiryani() {
                        System.out.println("Chicken Biryani");
                        return "ChickenBiryani";
            }

           
            public String getFishBiryani() {
                        System.out.println("Chicken Biryani");
                        return "ChickenBiryani";
            }
}





public class Client {
            public static void main(String... args) {
             //Ramesh Wants to order - 2 doshas and 2 Idlies from Udupi System
                        VegService vegService = new VegService();
                        String vegCombo = vegService.getDosha();
                        vegCombo += vegService.getIdly();
                        vegCombo += vegService.getDosha();
                        vegCombo += vegService.getIdly();
                       
            //Ramesh Wants to order - 1 chicken and 1 mutton biryani from Krutunga System
                        NonvegService nonVegService = new NonvegService();
                        String nonVegCombo = nonVegService.getChickenBiryani();
                        nonVegCombo += nonVegService.getMuttonBiryani();               
            }
}


Client orders 2 doshas and 2 idlies from Udupi System
              1 chicken and 1 mutton from Krutunga System

So Client making 2 doshas (2) + 2 idlies = 4 Remote Calls to Udupi System
   Client making 1 chicken (1) + 1 mutton (1) = 2 Remote Calls to Krutunga System.


Now ZomatoServiceFacade came into market, This ZomatoServieFacade will taken care of Udupi System and Krutunga System.

Now Client will contact ZomatoServiceFacade instead of direct call to Udupi and Krutunga System.


public class ZomatoServiceFacade {
           
            //System - Udupi Veg Service
            VegService vegService = new VegService();
           
            //System - Krutunga Nonveg Service
            NonvegService nonVegService = new NonvegService();
           
            public String vegCombo() {
                        String vegCombo = vegService.getDosha();
                        vegCombo += vegService.getIdly();
                        vegCombo += vegService.getDosha();
                        vegCombo += vegService.getIdly();
                        return vegCombo;
            }
           
            public String nonVegCombo() {
                        String nonVegCombo = nonVegService.getChickenBiryani();
                        nonVegCombo += nonVegService.getMuttonBiryani();
                        return nonVegCombo;
            }
           

}


public class Client {
           
            public static void main(String... args) {
                        ZomatoServiceFacade zomatoService = new ZomatoServiceFacade();
                        String vegCombo = zomatoService.vegCombo();
                        String nonVegCombo = zomatoService.nonVegCombo();
            }
}



Now Client Calling the ZomatoServiceFacade Service for vegCombo and nonVegCombo which will suitable for our requirement.

Client – 1 vegCombo + 1 nonVegCombo = Total 2 Remote Calls to ZomatoService.

Due to Façade Remotecalls reduced.







No comments:

Post a Comment