Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / operating-systems / Windows / Win8

Façade Design Pattern

0.00/5 (No votes)
24 Sep 2014CPOL2 min read 10.8K  
Façade design pattern

What Does Façade Mean ??

A facade or façade is generally one exterior side of a building, usually, but not always, the front. The word comes from the French language, literally meaning “frontage” or “face”.

GOF –> 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.

Participants

  1. Facade: Knows which subsystem classes are responsible for a request. Delegates client requests to appropriate subsystem objects.
  2. Subsystem classes: Implements subsystem functionality. Handle work assigned by Facade object. Have no knowledge of the facade; that is, they keep no references to it.

Let us look at a simple representation of this pattern.

Facade

Now that we have understood what a Facade actually is and what GOF have interpreted with this, we will go ahead and look at a very simple and real world application of this pattern.

This is an abstract example of how a client (“you”) interacts with a facade (the “computer”) to a complex system (internal computer parts, like CPU and HardDrive).

The operations are somewhat as given below:

  1. Processor is first frozen.
  2. Memory is then loaded with the help of Hardrive information.
  3. Processor then jumps to the location returned by the memory.
  4. Processor then executes from that particular location.

First, we will see with the Facade pattern how the code looks like:

C#
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) { ... }
}

The above classes, viz. CPU, Memory and HardDrive form the SUB-SYSTEMS.
Now if we were to write a client to call these methods, it would look something like below:

C#
class Client {
public static void main(String[] args) {
        CPU mycpu = new CPU();
    Memory mymemory = new Memory();
    HardDrive hd = new HardDrive();

    mycpu.freeze();
    mymemory.load(BOOT_ADDRESS, hd.read(BOOT_SECTOR,     SECTOR_SIZE));
    mycpu.jump(BOOT_ADDRESS);
    mycpu.execute();
    }
}

So from the above code:

  1. The client knows about the subsystem implementation which is incorrect.
  2. There is a strong coupling between Client and the subsystem which is incorrect.
  3. There is no generality to the code, thus you need to keep disturbing client code even if the subsystem code changes.

Thus to avoid all the above problems, we will implement the same using a Facade design pattern.

Again, we will look at the subsystem which will remain the same.

C#
/* 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) { ... }
}

Now we will look at the Facade object participant which comes into the picture.

C#
/* Facade */
 
class ComputerFacade {
    private CPU processor;
    private Memory ram;
    private 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();
    }
}

Thus, we have clean implementation and a single object of this class will solve all our problems. Thus this class abstracts the subsystems.

Now we will look at the client.

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

Usage

  1. Provide simple interface to complex subsystems
  2. When there are many dependencies between clients and implementation class
  3. You want to neatly layer your subsystem


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)