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

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

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

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

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

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.

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

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


That’s all. Thanks for your reading.

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.