Archive

Archive for October, 2015

Facade Pattern

October 26, 2015 Leave a comment

Facade pattern hides complexities of the system and provides an system interface to the client to interact with. System interface are not aware with facade. Here is example

Button.java


public interface Button {

public String press();

public String release();

}

HappyButton.java

public class HappyButton implements Button {

 public String press() {
 return "Happy button is being press";
 }

 public String release() {
 return "Happy button is relased";
 }

}

PanicButton.java

public class PanicButton implements Button {

 public String press() {
 return "Panic button is being press";
 }

 public String release() {
 return "Panic button is released";
 }

}

NormalButton.java

public class NormalButton implements Button {

 public String press() {
 return "Normal button is being press";
 }

 public String release() {
 return "Normal button is released";
 }

}

FacadeHelper.java

public class FacadeHelper {

 private Button happyButton;
 private Button panicButton;
 private Button normalButton;

 public FacadeHelper() {
 happyButton = new HappyButton();
 panicButton = new PanicButton();
 normalButton = new NormalButton();
 }

 public String PanicButtonPress() {
 return panicButton.press();
 }

 public String PanicButtonRelease() {
 return panicButton.release();
 }

 public String HappyButtonPress() {
 return happyButton.press();
 }

 public String HappyButtonRelease() {
 return happyButton.release();
 }

 public String NormalButtonPress() {
 return normalButton.press();
 }

 public String NormalButtonRelease() {
 return normalButton.release();
 }
}

FacadeClient.java

public class FacadeClient {
 public static void main(String[] args){
 FacadeHelper facadeHelper = new FacadeHelper();

 System.out.println(facadeHelper.HappyButtonPress());
 System.out.println(facadeHelper.HappyButtonRelease());
 System.out.println(facadeHelper.NormalButtonPress());
 System.out.println(facadeHelper.NormalButtonRelease());
 System.out.println(facadeHelper.PanicButtonPress());
 System.out.println(facadeHelper.PanicButtonRelease());
 }
}

Result:

Facade Desing Pattern

Facade Desing Pattern

Advertisements