Friday, October 31, 2014

Facade design pattern example

Façade è Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system.

Example : Suppose a customer want to know the availability of a specific maruti car (say AltoK10 or Alto800 or Swift) in a maruti showroom. So he/she simply ask the showroom representative for the availability. Now showroom representative checked the availability and tell the user about available quantity.
Here showroom representative acts a FAÇADE.

----------------------------------------------------
package com.exmpl.facade;

public interface Maruti {
       void getAvailability();
}
----------------------------------------------------
package com.exmpl.facade;

public class AltoK10 implements Maruti {

       @Override
       public void getAvailability() {
              System.out
                           .println("There are FIVE piece of Alto K10 are available in our show room.");
       }
}
----------------------------------------------------
package com.exmpl.facade;

public class Alto800 implements Maruti {

       @Override
       public void getAvailability() {
              System.out
                           .println("There are TWENTY piece of Alto 800 are available in our show room.");
       }

}
----------------------------------------------------
package com.exmpl.facade;

public class Swift implements Maruti {

       @Override
       public void getAvailability() {
              System.out.println("SWIFT is out of stock in our show room.");
       }
}
----------------------------------------------------
// ########### Facade class ###########
package com.exmpl.facade;

public class ShowRoomRepresentative {
       private Maruti altoK10;
       private Maruti alto800;
       private Maruti swift;

       public ShowRoomRepresentative() {
              altoK10 = new AltoK10();
              alto800 = new Alto800();
              swift = new Swift();
       }

       public void checkAltoK10Availability() {
              altoK10.getAvailability();
       }

       public void checkAlto800Availability() {
              alto800.getAvailability();
       }

       public void checkSwiftAvailability() {
              swift.getAvailability();
       }
}
----------------------------------------------------
package com.exmpl.facade;

public class Customer {

       public static void main(String[] args) {
              ShowRoomRepresentative representative = new ShowRoomRepresentative();
              System.out.println("Customer : How many Alto K10 is available?");
              representative.checkAltoK10Availability();
              System.out.println("Customer : How many Alto 800 is available?");
              representative.checkAlto800Availability();
              System.out.println("Customer : How many Swift is available?");
              representative.checkSwiftAvailability();
       }

}
----------------------------------------------------
Output è
Customer : How many Alto K10 is available?
There are FIVE piece of Alto K10 are available in our show room.
Customer : How many Alto 800 is available?
There are TWENTY piece of Alto 800 are available in our show room.
Customer : How many Swift is available?

SWIFT is out of stock in our show room.

No comments:

Post a Comment