Facade Pattern

Applications reach different levels of complexity within the development time span. Some settle down with just several classes. Others make to dozens of classes and complex interrelations between them. When that happens you want to make sure that you have proper mechanisms of controlling it, and that those mechanisms are simple enough for client to interact with.

Imagine you wrote classes and interfaces that represent different parts of automobile: engine, radiator, wheel system, battery, and many others. If you wanted client to be able to use the automobile as a whole, they would need to pull all classes together, and then call methods and functions in correct order all by themselves. That brings a lot of complexity and also unnecessary errors, and time wasted. How about we do all that work for our client, meaning we would expose several simple methods to our client, like startUp()/stop(), steerLeft()/steerRight(), accelerate()/brake(), and a couple more. Those methods would incorporate all the intricate details about how to make wheels turn, and which systems to invoke during that process, so that client wouldn’t need to worry about that. And that is what Facade pattern does.


The Facade Pattern:

provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.


Facade not only simplifies the whole interaction, it also decouples the client from the details of implementation of classes. This is a pretty straightforward pattern that makes things a little easier.

// Computer Example in Java
// Source: https://en.wikipedia.org/wiki/Facade_pattern
/* Complex parts */

class CPU {
    public void freeze() { ... }
    public void jump(long position) { ... }
    public void execute() { ... }
}

class HardDrive {
    public byte[] read(long lba, int size) { ... }
}

class Memory {
    public void load(long position, byte[] data) { ... }
}

/* Facade */

class ComputerFacade {
    private final CPU processor;
    private final Memory ram;
    private final HardDrive hd;

    public ComputerFacade() {
        this.processor = new CPU();
        this.ram = new Memory();
        this.hd = new HardDrive();
    }

    public void start() {
        processor.freeze();
        ram.load(BOOT_ADDRESS, hd.read(BOOT_SECTOR, SECTOR_SIZE));
        processor.jump(BOOT_ADDRESS);
        processor.execute();
    }
}

/* Client */

class You {
    public static void main(String[] args) {
        ComputerFacade computer = new ComputerFacade();
        computer.start();
    }
}

Facade implements the Principle of Least Knowledge, which states: “talk only to your immediate friends”. This concept narrows down the scope with which we have to approach development of classes, and makes sure we make them the least coupled possible. You do not want to build something that is so heavily intertwined that it is difficult to maintain it, and further its development. Facade pattern enables client to be its only “friend”, thus isolating other classes from it, by taking the responsibility of talking to those. Client gets simple interface and only talks to the Facade. What’s even better is that if you want to keep developing your subsystems or add another one, like if you wanted to install extra jet engine on your car, you can do that without affecting the client.

// Computer Example in PHP
// Source: https://en.wikipedia.org/wiki/Facade_pattern
/**
 * The complicated, underlying logic.
 */
class CPU
{
    public function freeze() {/* ... */}
    public function jump($position) {/* ... */}
    public function execute() {/* ... */}
}

class Memory
{
    public function load($position, $data) {/* ... */}
}

class HardDrive
{
    public function read($lba, $size) {/* ... */}
}

/**
 * The facade that users would be interacting with.
 */
class ComputerFacade
{
    protected $cpu;
    protected $memory;
    protected $hd;

    public function __construct()
    {
        $this->cpu = new CPU;
        $this->memory = new Memory;
        $this->hd = new HardDrive;
    }

    public function start()
    {
        $this->cpu->freeze();
        $this->memory->load(BOOT_ADDRESS, $this->hd->read(BOOT_SECTOR, SECTOR_SIZE));
        $this->cpu->jump(BOOT_ADDRESS);
        $this->cpu->execute();
    }
}

/**
 * How a user could start the computer.
 */
$computer = new ComputerFacade;
$computer->start();

Facade Pattern can be used in variety of places. Often times, a good indication that you need one might be the high complexity and abundance of classes around. Facades do not only isolate the client that uses the system. You can make a Facade inside the system itself, to simplify communication between those classes.

One thought on “Facade Pattern

Leave a comment