Facade

Author      Ter-Petrosyan Hakob

The Facade pattern is a structural design pattern that provides a simplified interface to a set of complex subsystems. It makes the system easier to use by hiding internal complexity behind a single entry point. While similar to the Adapter and Decorator patterns, the primary goal of a facade is to offer one clear interface to multiple features.

Motivation

As software grows in complexity, clients may need to interact with many classes or subsystems in a specific sequence. Without a facade, clients must know the subsystem APIs and the order of calls, leading to tight coupling and fragile code. A facade decouples clients from subsystem classes and reduces the number of dependencies.

Structure

img1

Example: Starting a Computer

Here is a Java implementation demonstrating the facade:

// Subsystem 1
class CPU {
    public void freeze()         { /* ... */ }
    public void jump(long pos)   { /* ... */ }
    public void execute()        { /* ... */ }
}

// Subsystem 2
class Memory {
    public void load(long pos, byte[] data) { /* ... */ }
}

// Subsystem 3
class HardDrive {
    public byte[] read(long lba, int size)  { /* ... */ return new byte[size]; }
}

// Facade
public class ComputerFacade {
    private final CPU cpu = new CPU();
    private final Memory memory = new Memory();
    private final HardDrive hardDrive = new HardDrive();
    private static final long BOOT_ADDRESS = 0x00;
    private static final long BOOT_SECTOR  = 0x10;
    private static final int SECTOR_SIZE   = 512;

    public void start() {
        cpu.freeze();
        byte[] bootData = hardDrive.read(BOOT_SECTOR, SECTOR_SIZE);
        memory.load(BOOT_ADDRESS, bootData);
        cpu.jump(BOOT_ADDRESS);
        cpu.execute();
    }
}

// Client
public class Main {
    public static void main(String[] args) {
        new ComputerFacade().start();
    }
}

Sequence Diagram

img2

When to Use

Pros&Cons

Pros:

Cons:

Implementation Tips