Facade pattern

From Wikipedia
Jump to: navigation, search

The facade pattern (or façade pattern) is a software design pattern commonly used with object-oriented programming. The name is by analogy to an architectural facade.

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

  • make a software library easier to use, understand and test, since the facade has convenient methods for common tasks;
  • make the library more readable, for the same reason;
  • reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system;
  • wrap a poorly designed collection of APIs with a single well-designed API (as per task needs).

Contents

Usage

An adapter is used when the wrapper must respect a particular interface and must support polymorphic behavior. A decorator makes it possible to add or alter behavior of an interface at run-time. On the other hand, a facade is used when one wants an easier or simpler interface.<ref>Template:Cite journal</ref>

Pattern Intent
Adapter Converts one interface to another so that it matches what the client is expecting
Decorator Dynamically adds responsibility to the interface by wrapping the original code
Facade Provides a simplified interface

Structure

FacadeDesignPattern.png

Facade
The facade class abstracts Packages 1, 2, and 3 from the rest of the application.
Clients
The objects are using the Facade Pattern to access resources from the Packages.

Example

Template:Wikibooks

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).

<source lang="java"> /* 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 You {

   public static void main(String[] args) {
       Computer facade = new Computer();

facade.startComputer();

   }

} </source>

References

Template:Reflist

External links

Template:Commons category

Template:Design Patterns Patternsbg:Фасада (шаблон) de:Fassade (Entwurfsmuster) es:Facade (patrón de diseño) fr:Façade (patron de conception) gl:Facade (patrón de deseño) ko:퍼사드 패턴 it:Façade pattern mn:Факэдэ загвар nl:Façade (ontwerppatroon) ja:Facade パターン pl:Fasada (wzorzec projektowy) pt:Façade ru:Фасад (шаблон проектирования) th:ฟะซาดแพตเทิร์น uk:Фасад (шаблон проектування) vi:Facade pattern zh:外觀模式

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox