Skip to content
Search

Facade pattern

lastmod 2026-06-17created 2015-09-234 edits

The facade pattern (French: façade[fəˈsɑːd] English: pattern) is one of the software engineering design patterns. It is often used in object-oriented programming.

A facade is an object that provides a simplified interface to a larger body of code in some software, such as a class library.

  • A facade makes a software library easier to use. A facade also makes a software library easier to understand. A facade provides convenient methods for common tasks.
  • A facade makes code that uses the library more readable.
  • A facade reduces the dependence of code outside the library on the library's internal code. Since most outside code uses the facade, it improves flexibility in developing the system.
  • A facade wraps a collection of poorly designed APIs in a single well-designed API.

Use the adapter pattern when the wrapper must conform to a particular interface and must support polymorphic behavior. Use a facade when you simply want an easier, simpler interface.

Structure

FacadeDesignPattern.png

Facade
The facade class interacts with packages 1, 2, 3 and with the rest of the application code not shown in the figure.
Clients
Objects that use the facade class to access resources in the packages.
Packages
A software library / collection of APIs. Accessed through the facade class.

Java example

The Java code example below is an abstract example in which the user (you) accesses the parts inside a computer (CPU, HDD, etc.) through a facade (the computer).

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

/* Façade */

class Computer {
    public void startComputer() {
                CPU cpu = new CPU();
                Memory memory = new Memory();
                HardDrive hardDrive = new HardDreive();
        cpu.freeze();
        memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));
        cpu.jump(BOOT_ADDRESS);
        cpu.execute();
    }
}

/* Client */

class You {
    public static void main(String[] args) throws ParseException {
        Computer facade = /* grab a facade instance */;
        facade.startComputer();
    }
}

Favorite site

author Zer0 sitemap