Façade design patterns

Hiding complexity between your classes and interfaces is key concept for implementation of Facade Design Pattern.

Provide a unified interface to a set of interfaces in a subsystem. Façade Pattern defines a higher-level interface that makes the subsystem easier to use. – Gof Definition

Here Façade pattern is not implemented

Here we are talking about the application where Interfaces and classes are interaction is very huge, which may cause several performance related issues, so to simplify the interaction and minimize the interaction we used to define an extra layer to it, which usually work as mediator to both interface giants, like controller in 
MVC architecture but the difference is that it should not work like layer that imposes security and hides important data and implementation.

To simplify the above issue we used have one more layer as displayed in below images:


Facts about Facade Design pattern:

  1. Facade provides a single interface.
  2. Programmers comfort is a main purpose of facade.
  3. Simplicity is the aim of facade pattern.
  4. Facade design pattern is used for promoting subsystem independence and portability.
  5. Subsystem may be dependent with one another. In such case, facade can act as a coordinator and decouple the dependencies between the subsystems.
  6. Translating data to suit the interface of a subsystem is done by the facade.

Beaware of Facade Design Patterns:

  • A Layer should not be force in between two layers to the sake of creating the Facade, use it ony where it is unnecessary.
  • Transfer application complexity to facade layer not one or two line, otherwise it will be a delegation and not Facade Layer.
  •  Facade Layer should be created in last, since it should be used to decrease the complexity, else you will create unnecessary interface to interact.

Example Code:

/* Complex parts */

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

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

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

/* Facade */

class Computer {
private CPU cpu;
private Memory memory;
private HardDrive hardDrive;

public Computer() {
this.cpu = new CPU();
this.memory = new Memory();
this.hardDrive = new HardDrive();
}

public void startComputer() {
cpu.freeze();
memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));
cpu.jump(BOOT_ADDRESS);
cpu.execute();
}
}

/* Client */

class People {
public static void main(String[] args) {
Computer facade = new Computer();
facade.startComputer();
}
}

If you like reading this please spare 5 seconds and share it with others or comment for improvement.