Thursday, July 8, 2010

Facade Design pattern

Facade pattern hides the complexities of the system and provides an interface to the client using which the 
client can access the system. Call Center people are representation of entire product which company promotes.
This pattern adds an interface to existing system to hide its complexities. 

The interface JDBC can be called a facade. We as users or clients create connection using the “java.sql.Connection” 
interface, the implementation of which we are not concerned about. The implementation is left to the vendor of driver.

-------------------------------------------------------
public interface Shape {
   void draw();
}

public class Rectangle implements Shape {
   @Override
   public void draw() {
      System.out.println("Rectangle::  draw()");
   }
}

public class Square implements Shape {
   @Override
   public void draw() {
      System.out.println("Square::  draw()");
   }
}

public class Circle implements Shape {
   @Override
   public void draw() {
      System.out.println("Circle::  draw()");
   }
}

public class ShapeMaker {
   private Shape circle;
   private Shape rectangle;
   private Shape square;

   public ShapeMaker() {
      circle = new Circle();
      rectangle = new Rectangle();
      square = new Square();
   }

   public void drawCircle(){
      circle.draw();
   }
   public void drawRectangle(){
      rectangle.draw();
   }
   public void drawSquare(){
      square.draw();
   }
}

//Main Program
public class FacadePatternDemo {
   public static void main(String[] args) {
      ShapeMaker shapeMaker = new ShapeMaker();

      shapeMaker.drawCircle();
      shapeMaker.drawRectangle();
      shapeMaker.drawSquare();        
   }
}
Code: https://www.tutorialspoint.com/design_pattern/facade_pattern.htm

No comments:

Post a Comment