Computer Science Design Patterns/Facade
From Wikibooks, open books for an open world
Contents |
[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; 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(); } }
[edit] C#
using System; namespace Facade { public class CPU { public void Freeze() { } public void Jump(long addr) { } public void Execute() { } } public class Memory { public void Load(long position, byte[] data) { } } public class HardDrive { public byte[] Read(long lba, int size) { return null; } } public class Computer { var cpu = new CPU(); var memory = new Memory(); var hardDrive = new HardDrive(); public void StartComputer() { cpu.Freeze(); memory.Load(0x22, hardDrive.Read(0x66, 0x99)); cpu.Jump(0x44); cpu.Execute(); } } public class SomeClass { public static void Main(string[] args) { var facade = new Computer(); 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] Javascript
/* Complex parts */ var CPU = function () {}; CPU.prototype = { freeze: function () { console.log('CPU: freeze'); }, jump: function (position) { console.log('CPU: jump to ' + position); }, execute: function () { console.log('CPU: execute'); } }; var Memory = function () {}; Memory.prototype = { load: function (position, data) { console.log('Memory: load "' + data + '" at ' + position); } }; var HardDrive = function () {}; HardDrive.prototype = { read: function (lba, size) { console.log('HardDrive: read sector ' + lba + '(' + size + ' bytes)'); return 'hdd data'; } }; /* Facade */ var Computer = function () { var cpu, memory, hardDrive; cpu = new CPU(); memory = new Memory(); hardDrive = new HardDrive(); var constant = function (name) { var constants = { BOOT_ADDRESS: 0, BOOT_SECTOR: 0, SECTOR_SIZE: 512 }; return constants[name]; }; this.startComputer = function () { cpu.freeze(); memory.load(constant('BOOT_ADDRESS'), hardDrive.read(constant('BOOT_SECTOR'), constant('SECTOR_SIZE'))); cpu.jump(constant('BOOT_ADDRESS')); cpu.execute(); } }; /* Client */ var facade = new Computer(); facade.startComputer();
[edit] ActionScript 3.0
/* Complex Parts */ /* CPU.as */ package { public class CPU { public function freeze():void { trace("CPU::freeze"); } public function jump(addr:Number):void { trace("CPU::jump to", String(addr)); } public function execute():void { trace("CPU::execute"); } } } /* Memory.as */ package { import flash.utils.ByteArray; public class Memory { public function load(position:Number, data:ByteArray):void { trace("Memory::load position:", position, "data:", data); } } } /* HardDrive.as */ package { import flash.utils.ByteArray; public class HardDrive { public function read(lba:Number, size:int):ByteArray { trace("HardDrive::read returning null"); return null; } } } /* The Facade */ /* Computer.as */ package { public class Computer { public static const BOOT_ADDRESS:Number = 0x22; public static const BOOT_SECTOR:Number = 0x66; public static const SECTOR_SIZE:int = 0x200; private var _cpu:CPU; private var _memory:Memory; private var _hardDrive:HardDrive; public function Computer() { _cpu = new CPU(); _memory = new Memory(); _hardDrive = new HardDrive(); } public function startComputer():void { _cpu.freeze(); _memory.load(BOOT_ADDRESS, _hardDrive.read(BOOT_SECTOR, SECTOR_SIZE)); _cpu.jump(BOOT_ADDRESS); _cpu.execute(); } } } /* Client.as : This is the application's Document class */ package { import flash.display.MovieClip; public class Client extends MovieClip { private var _computer:Computer; public function Client() { _computer = new Computer(); _computer.startComputer(); } } }