Introdocution

In software development, it's often important to simplify an API or to make drastic changes to the way a user interacts with the underlying system library. Whenever we do this we can write a facade to assemble parts, restructure/combine API calls, or mock out a api definition to work with our existing system.

Definition

Pattern that provides a unified interface to a set of interfaces in a subsystem.

A PHP 5 Example

/* 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();

Conclusion

The Facade Pattern allows us to make drastic library level changes without affecting the client level applications, it's also very useful for convenience methodology.

Further Reading

[http://en.wikipedia.org/wiki/Facade_pattern Wikipedia: Facade Pattern]

 
facade.txt · Last modified: 2010/05/18 02:00 by mfacenet
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki