Home > Java > Facade Pattern

Facade Pattern

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
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: