Facade Design Pattern in Java

This design pattern comes under structural pattern as this pattern adds an interface to the existing system to hide all its complexities. As the name suggests Facade means the face of the building block.

The real life examples of this pattern can be explained as explained below :

We know that Computer system starts up when we push a start button on the CPU but we do not know how that start button sends signal to other internal circuits to actually boot the computer system. So the Facade pattern just gives us a friendly start button to start up the computer and hides all internal complexities of how it happens.
We can also take an another example of starting a car. We just push the key and start the ignition switch to start the car and we are not bother about how that ignition switch (sends signal to other internal circuits) starts the car.

This is how facade pattern is used. It hides the complexities of the system and provides an interface to the client from where the client can access the system.
Design pattern of JDBC can be called as a facade pattern. Clients create connection using the java.sql.Connection interface, the implementation of which clients are not concerned about. The implementation is left to the vendor of driver.

Let’s try and understand the facade pattern better using a simple example. Let’s consider a starting mechanism of a car engine. We will see how car engine starts up once we turns the ignition switch on to the car.

You, as a client want to start the car engine so that car will be moving on. You do not know how the ignition switch starts the car engine. You just need to know how to turns the ignition switch on using the key and how to take the car from one place to another place. Here, the ignition switch acts as the facade, as it hides the complexities of the system functionality.

Class Diagram

Facade Pattern in Java

Example
Let us see using the below example how the starting mechanism of the car engine works.

We have several classes as shown in the below source codes

package com.roytuts.designpattern.facade;
public class IgnitionSwitch {
  public void sendSignal() {
    System.out.println("Ignition switch sends voltage signal");
  }
}

The above class, IgnitionSwitch, sends the voltage signal as soon as the ignition switch is turned on using the key.

package com.roytuts.designpattern.facade;
public class Circuit {
  public void startControlCircuit() {
    System.out.println("Starts the starter control circuit");
  }
  public void activateSolenoid() {
    System.out.println("Activates the starter solenoid");
  }
}

The class, Circuit, performs several tasks as shown in the methods.

package com.roytuts.designpattern.facade;
public class Motor {
  public void energize() {
    System.out.println("Energizes the starter motor");
  }
  public void spin() {
    System.out.println("Spins the motor");
  }
}

The above class, Motor, energizes and spins the Motor, not necessarily in orderly fashion.

package com.roytuts.designpattern.facade;
public class Engine {
  public void meshGearWithFlywheel() {
    System.out.println("Meshes starter gear forward with flywheel");
  }
  public void turnOnCrankshaft() {
    System.out.println("Turns on engine crankshaft");
  }
  public void startEngine() {
    System.out.println("Starts the Engine");
  }
}

The above class, Engine, also does several jobs but not necessarily in the same order as shown in the class methods.

Now we have the following CarFacade.java which does the complex implementation of the engine start up.

package com.roytuts.designpattern.facade;
public class CarFacade {
  private IgnitionSwitch ignitionSwitch;
  private Circuit circuit;
  private Engine engine;
  private Motor motor;
  public CarFacade() {
    this.ignitionSwitch = new IgnitionSwitch();
    this.circuit = new Circuit();
    this.engine = new Engine();
    this.motor = new Motor();
  }
  public void start() {
    ignitionSwitch.sendSignal();
    circuit.startControlCircuit();
    circuit.activateSolenoid();
    motor.energize();
    engine.meshGearWithFlywheel();
    motor.spin();
    engine.turnOnCrankshaft();
    engine.startEngine();
  }
}

Next we create a client class which just starts the ignition switch

package com.roytuts.designpattern.facade;
public class FacadePatternTest {
  /**
   * @param args
   */
  public static void main(String[] args) {
    CarFacade carFacade = new CarFacade();
    carFacade.start();
  }
}

Run the above class FacadePatternTest and look at the console output below.

Output

Ignition switch sends voltage signal
Starts the starter control circuit
Activates the starter solenoid
Energizes the starter motor
Meshes starter gear forward with flywheel
Spins the motor
Turns on engine crankshaft
Starts the Engine

That’s all. Thanks for your reading.

Leave a Reply

Your email address will not be published. Required fields are marked *