Java Design Patterns Tutorial - Java Design Pattern - Facade Pattern








Facade pattern hides the complexities of a system.

It provides a simple interface to the client and the client uses the interface to interact with the system.

Facade pattern is a structural pattern.

Example

class ShapeFacade {
  interface Shape {
    void draw();//  w w  w . j a  va2 s .  c  o m
  }
  class Rectangle implements Shape {
    @Override
    public void draw() {
      System.out.println("Rectangle::draw()");
    }
  }
  class Square implements Shape {
    @Override
    public void draw() {
      System.out.println("Square::draw()");
    }
  }
  class Circle implements Shape {
    @Override
    public void draw() {
      System.out.println("Circle::draw()");
    }
  }
  private Shape circle = new Circle();
  private Shape rectangle = new Rectangle();
  private Shape square = new Square();

  public ShapeFacade() {
  }
  public void drawCircle() {
    circle.draw();
  }
  public void drawRectangle() {
    rectangle.draw();
  }
  public void drawSquare() {
    square.draw();
  }
}
public class Main {
  public static void main(String[] args) {
    ShapeFacade shapeFacade = new ShapeFacade();
    shapeFacade.drawCircle();
    shapeFacade.drawRectangle();
    shapeFacade.drawSquare();
  }
}

The code above generates the following result.