Java Design Pattern: Facade

Facade design pattern hides complexity of a task and provides a simple interface. The startup of a computer is a good example. When a computer starts up, it involves the work of cpu, memory, hard drive, etc. To make it easy to use for users, we can add a facade which wrap the complexity of the task, and provide one simple interface instead.

1. Facade Pattern Class Diagram


2. Java Facade Pattern Example

//the components of a computer
class CPU {
    public void processData() { }
class Memory {
    public void load() { }
class HardDrive {
    public void readdata() { }
/* 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 run() {
class User {
    public static void main(String[] args) {
        Computer computer = new Computer();;

3. Example in Real Project

In javax.faces.context, ExternalContext internally uses ServletContext, HttpSession, HttpServletRequest, HttpServletResponse, etc. It allows the Faces API to be unaware of the nature of its containing application environment.

This example is based on Facade design pattern in Wiki, so the credit is given to Wiki.

1. ExternalContext
2. Wiki Facade

Category >> Design Patterns Stories  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";