Tuesday, May 31, 2016

Facade Design Pattern tutorial in java

What is Facade Design Pattern?

Facade is a structural Design Pattern and In java the interface JDBC is an example of facade pattern.we as a user creates connection using the java.sql.connection interface,the implementation of which we are not concerned about.

Let us consider a simple example for understanding the pattern.while walking past the road we can only see this glass face of the building.we do not know anything about it,the wiring,the pipes and other complexities.
The face hides all the complexity of the building and displays a good looking front.

Facade design is one among the other design patterns that promotes loose coupling.

Implementation:

/**
 * @author Abhinaw.Tripathi
 *
 */

class GUIMenu
{
  public void drawMenuButton()
  {  
  }
}
class GUITitleBar
{
  public void showTitleBar(String caption)
  {  
  }
}

class GUIContent
{
  public void showButton()
  {  
  }

  public void showtextFeilds()
  {

  }

  public void setDefaultsValues()
  {

  }
}


class MyGUI
{
 private GUIMenu menu;
 private GUITitleBar titleBar;
 private GUIContent content;

 public MyGUI() {
// TODO Auto-generated constructor stub
menu=new GUIMenu();
titleBar=new GUITitleBar();
content=new GUIContent();
}

 public void drawGUI()
 {

content.showButton();
content.setDefaultsValues();
content.showtextFeilds();
menu.drawMenuButton();
titleBar.showTitleBar("Title of the GUI");
 }


}

public class FacadeDesignTest {

/**
*
*/
public FacadeDesignTest() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyGUI facade=new MyGUI();
facade.drawGUI();

}


}

When to Use Facade Design Pattern:
  • you want to provide a simple interface to a complex subsystem.
  • there are many dependencies between clients and the implementation classes of an abstraction.
  • you want to layer your subsystems.Use a facade to define an entry point to each subsystem level.
  • Facade provides a single interface.

No comments: