Facade Design Pattern in Java

I just announced the new Spring 5 modules in REST With Spring:

1. Introduction

In this quick tutorial, we’re going to take a look at one of the structural design patterns: the Facade.

First, we’ll give an overview of the pattern, list its benefits and describe what problems it solves.

Then, we’ll apply the facade pattern to an existing, practical problem with Java.

2. What is a Facade?

Simply put, a facade encapsulates a complex subsystem behind a simple interface. It hides much of the complexity and makes the subsystem easy to use.

Also, if we need to use the complex subsystem directly, we still can do that; we aren’t forced to use the facade all the time.

Besides a much simpler interface, there’s one more benefit of using this design pattern. It decouples a client implementation from the complex subsystem. Thanks to this, we can make changes to the existing subsystem and don’t affect a client.

Let’s see the facade in action.

3. Example

Let’s say that we want to start a car. The following diagram represents the legacy system, which allows us to do so:

As you can see, it can be quite complex and does require some effort to start the engine correctly: