Java Design Patterns Facade Patterns

Introduction

Facade Patterns provide a unified interface to a set of interfaces.

Use case

  • Provide integrated interface for control the system

Example

class WindowBorder {
   private String type;

   public void setMetal(String t) {
      this.type = t;
   }/*  ww  w .  j  a  v  a 2 s  .c o  m*/

   public String getType() {
      return this.type;
   }
}

class WindowFacade {
   WindowColor windowColor;
   WindowBorder windowBorder;
   WindowBody windowBody;

   public WindowFacade() {
      windowColor = new WindowColor();
      windowBorder = new WindowBorder();
      windowBody = new WindowBody();

   }

   public void ConstructWindow(String color, String metal) {
      windowColor.setColor(color);
      windowBorder.setMetal(metal);
      windowBody.createBody();
   }
}

class WindowColor {
   private String color;

   public void setColor(String color) {
      this.color = color;
      System.out.println("Color is set to : " + this.color);
   }
}

class WindowBody {
   public void createBody() {
      System.out.println("Body Creation done");
   }
}

public class Main {
   public static void main(String[] args) {
      WindowFacade rf1 = new WindowFacade();
      rf1.ConstructWindow("Green", "Iron");
      WindowFacade rf2 = new WindowFacade();
      rf2.ConstructWindow("Blue", "Steel");

   }
}



PreviousNext

Related