Facade pattern
From Wikipedia, the free encyclopedia
The facade pattern is a software engineering 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 and understand, since the facade has convenient methods for common tasks;
- make code that uses 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).
An Adapter is used when the wrapper must respect a particular interface and must support a polymorphic behavior. On the other hand, a facade is used when one wants an easier or simpler interface to work with.: Software library / API collection accessed through the Facade Class.
Contents |
[edit] Structure
- Facade
- The facade class abstracts Packages 1, 2, and 3 from the rest of the application.
- Clients
- The objects using the Facade Pattern to access resources from the Packages.
- Packages
[edit] Examples
[edit] Java
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).
/* 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=null; private Memory memory=null; private HardDrive hardDrive=null; public Computer(CPU c, Memory m, HardDrive hd) { this.cpu=c; this.memory=m; this.hardDrive=hd; } 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(new CPU(), new Memory(), new HardDrive()); facade.startComputer(); } }
[edit] Python
# Complex parts class CPU: def freeze(self): pass def jump(self, position): pass def execute(self): pass class Memory: def load(self, position, data): pass class HardDrive: def read(self, lba, size): pass # Facade class Computer: def __init__(self): self.cpu = CPU() self.memory = Memory() self.hard_drive = HardDrive() def start_computer(self): self.cpu.freeze() self.memory.load(0, self.hard_drive.read(0, 1024)) self.cpu.jump(10) self.cpu.execute() # Client if __name__ == '__main__': facade = Computer() facade.start_computer()
[edit] PHP
/* Complex parts */ class CPU { public function freeze() { /* ... */ } public function jump( $position ) { /* ... */ } public function execute() { /* ... */ } } class Memory { public function load( $position, $data ) { /* ... */ } } class HardDrive { public function read( $lba, $size ) { /* ... */ } } /* Facade */ class Computer { protected $cpu = null; protected $memory = null; protected $hardDrive = null; public function __construct() { $this->cpu = new CPU(); $this->memory = new Memory(); $this->hardDrive = new HardDrive(); } public function startComputer() { $this->cpu->freeze(); $this->memory->load( BOOT_ADDRESS, $this->hardDrive->read( BOOT_SECTOR, SECTOR_SIZE ) ); $this->cpu->jump( BOOT_ADDRESS ); $this->cpu->execute(); } } /* Client */ $facade = new Computer(); $facade->startComputer();
[edit] External links
|
|||||||||||
