Easy Tutorial
❮ Design Pattern Tutorial Proxy Pattern ❯

Facade Pattern

The Facade Pattern hides the complexities of the system and provides an interface to the client using which the client can access the system. This type of design pattern comes under structural pattern as it adds an interface to the existing system to hide its complexities.

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

Introduction

Intent: Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Main Problem: To reduce the complexity of accessing internal subsystems of a complex system, simplifying the interface between the client and the system.

When to Use:

  1. The client does not need to know the complex internal connections of the system; the entire system only needs to provide a "receptionist".
  2. Define the entry point of the system.

How to Solve: The client is not coupled with the system, but the facade class is coupled with the system.

Key Code: Add an additional layer between the client and the complex system, which handles the sequence of calls, dependencies, etc.

Example:

  1. Going to a hospital for treatment might involve registration, outpatient services, pricing, and medication pickup, which can be complex for patients or their families. If a receptionist is provided to handle these tasks, it becomes convenient.
  2. The three-tier development model in JAVA.

Advantages:

  1. Reduces system interdependencies.
  2. Increases flexibility.
  3. Enhances security.

Disadvantages: Does not conform to the open-closed principle; modifying something can be troublesome, and inheritance or overriding is not suitable.

Usage:

  1. Provide a module for external access to a complex module or subsystem.
  2. The subsystem is relatively independent.
  3. Prevent risks brought by low-level personnel.

Notes: In a hierarchical structure, the facade pattern can be used to define the entry point for each layer of the system.

Implementation

We will create a Shape interface and concrete classes implementing the Shape interface. Next, we define a facade class ShapeMaker.

The ShapeMaker class uses the concrete classes to delegate user calls to these classes. The FacadePatternDemo class uses the ShapeMaker class to show the results.

Step 1

Create an interface.

public interface Shape {
   void draw();
}

Step 2

Create concrete classes implementing the interface.

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()");
   }
}

Step 3

Create a facade class.

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();
   }
}

Step 4

Use the facade class to draw various types of shapes.

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

      shapeMaker.drawCircle();
      shapeMaker.drawRectangle();
      shapeMaker.drawSquare();      
   }
}

Step 5

Execute the program, outputting the result:

Circle::draw()
Rectangle::draw()
Square::draw()
❮ Design Pattern Tutorial Proxy Pattern ❯