Browser docs

Facade

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.

Explanation

Real-world example

How does a goldmine work? “Well, the miners go down there and dig gold!” you say. That is what you believe because you are using a simple interface that goldmine provides on the outside, internally it has to do a lot of stuff to make it happen. This simple interface to the complex subsystem is a facade.

In plain words

Facade pattern provides a simplified interface to a complex subsystem.

Wikipedia says

A facade is an object that provides a simplified interface to a larger body of code, such as a class library.

Programmatic Example

Let’s take our goldmine example from above. Here we have the dwarven mine worker hierarchy. First there’s a base class DwarvenMineWorker:

 1
 2@Slf4j
 3public abstract class DwarvenMineWorker {
 4
 5    public void goToSleep() {
 6        LOGGER.info("{} goes to sleep.", name());
 7    }
 8
 9    public void wakeUp() {
10        LOGGER.info("{} wakes up.", name());
11    }
12
13    public void goHome() {
14        LOGGER.info("{} goes home.", name());
15    }
16
17    public void goToMine() {
18        LOGGER.info("{} goes to the mine.", name());
19    }
20
21    private void action(Action action) {
22        switch (action) {
23            case GO_TO_SLEEP -> goToSleep();
24            case WAKE_UP -> wakeUp();
25            case GO_HOME -> goHome();
26            case GO_TO_MINE -> goToMine();
27            case WORK -> work();
28            default -> LOGGER.info("Undefined action");
29        }
30    }
31
32    public void action(Action... actions) {
33        Arrays.stream(actions).forEach(this::action);
34    }
35
36    public abstract void work();
37
38    public abstract String name();
39
40    enum Action {
41        GO_TO_SLEEP, WAKE_UP, GO_HOME, GO_TO_MINE, WORK
42    }
43}

Then we have the concrete dwarf classes DwarvenTunnelDigger, DwarvenGoldDigger and DwarvenCartOperator:

 1@Slf4j
 2public class DwarvenTunnelDigger extends DwarvenMineWorker {
 3
 4  @Override
 5  public void work() {
 6    LOGGER.info("{} creates another promising tunnel.", name());
 7  }
 8
 9  @Override
10  public String name() {
11    return "Dwarven tunnel digger";
12  }
13}
14
15@Slf4j
16public class DwarvenGoldDigger extends DwarvenMineWorker {
17
18  @Override
19  public void work() {
20    LOGGER.info("{} digs for gold.", name());
21  }
22
23  @Override
24  public String name() {
25    return "Dwarf gold digger";
26  }
27}
28
29@Slf4j
30public class DwarvenCartOperator extends DwarvenMineWorker {
31
32  @Override
33  public void work() {
34    LOGGER.info("{} moves gold chunks out of the mine.", name());
35  }
36
37  @Override
38  public String name() {
39    return "Dwarf cart operator";
40  }
41}

To operate all these goldmine workers we have the DwarvenGoldmineFacade:

 1public class DwarvenGoldmineFacade {
 2
 3  private final List<DwarvenMineWorker> workers;
 4
 5  public DwarvenGoldmineFacade() {
 6      workers = List.of(
 7            new DwarvenGoldDigger(),
 8            new DwarvenCartOperator(),
 9            new DwarvenTunnelDigger());
10  }
11
12  public void startNewDay() {
13    makeActions(workers, DwarvenMineWorker.Action.WAKE_UP, DwarvenMineWorker.Action.GO_TO_MINE);
14  }
15
16  public void digOutGold() {
17    makeActions(workers, DwarvenMineWorker.Action.WORK);
18  }
19
20  public void endDay() {
21    makeActions(workers, DwarvenMineWorker.Action.GO_HOME, DwarvenMineWorker.Action.GO_TO_SLEEP);
22  }
23
24  private static void makeActions(Collection<DwarvenMineWorker> workers,
25      DwarvenMineWorker.Action... actions) {
26    workers.forEach(worker -> worker.action(actions));
27  }
28}

Now let’s use the facade:

1var facade = new DwarvenGoldmineFacade();
2facade.startNewDay();
3facade.digOutGold();
4facade.endDay();

Program output:

 1// Dwarf gold digger wakes up.
 2// Dwarf gold digger goes to the mine.
 3// Dwarf cart operator wakes up.
 4// Dwarf cart operator goes to the mine.
 5// Dwarven tunnel digger wakes up.
 6// Dwarven tunnel digger goes to the mine.
 7// Dwarf gold digger digs for gold.
 8// Dwarf cart operator moves gold chunks out of the mine.
 9// Dwarven tunnel digger creates another promising tunnel.
10// Dwarf gold digger goes home.
11// Dwarf gold digger goes to sleep.
12// Dwarf cart operator goes home.
13// Dwarf cart operator goes to sleep.
14// Dwarven tunnel digger goes home.
15// Dwarven tunnel digger goes to sleep.

Class diagram

alt text

Applicability

Use the Facade pattern when

  • You want to provide a simple interface to a complex subsystem. Subsystems often get more complex as they evolve. Most patterns, when applied, result in more and smaller classes. This makes the subsystem more reusable and easier to customize, but it also becomes harder to use for clients that don’t need to customize it. A facade can provide a simple default view of the subsystem that is good enough for most clients. Only clients needing more customization will need to look beyond the facade.
  • There are many dependencies between clients and the implementation classes of an abstraction. Introduce a facade to decouple the subsystem from clients and other subsystems, thereby promoting subsystem independence and portability.
  • You want to layer your subsystems. Use a facade to define an entry point to each subsystem level. If subsystems are dependent, then you can simplify the dependencies between them by making them communicate with each other solely through their facades.

Credits