Facade

A single class that represents an entire subsystem.

  • Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system

  • This pattern involves a single class which provides simplified methods required by client and delegates calls to methods of existing system classes.

  • Most frequently used in JavaScript.

EX:
interface Shape {
   draw(): void;
}

public class Rectangle implements Shape {
   draw() {
      console.log("Rectangle::draw()");
   }
}

public class Square implements Shape {
   draw() {
      console.log("Square::draw()");
   }
}

public class Circle implements Shape {
   draw() {
      console.log("Circle::draw()");
   }
}

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

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

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

const shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();	

Last updated