Skip to content

Instantly share code, notes, and snippets.

@hastinbe
Created September 13, 2019 01:50
Show Gist options
  • Save hastinbe/299f50b4272e4bb3a0df706bc1b525e8 to your computer and use it in GitHub Desktop.
Save hastinbe/299f50b4272e4bb3a0df706bc1b525e8 to your computer and use it in GitHub Desktop.
Facade Pattern Example #php #design-patterns #facade-pattern
<?php
const BOOT_ADDRESS = 0xd34db33f;
const BOOT_SECTOR = 0x7c;
const SECTOR_SIZE = 4096;
interface IComputer
{
public function powerOn();
public function powerOff();
}
class Memory
{
public function load($address, $data)
{
printf("load 0x%x, 0x%x\n", $address, $data);
}
}
class HardDrive
{
public function read($lba, $size)
{
printf("read 0x%x, 0x%x\n", $lba, $size);
return 0x80;
}
}
class Cpu
{
public function jump($position)
{
printf("jmp 0x%x\n", $position);
}
public function exec()
{
echo "exec\n";
}
}
class ComputerFacade implements IComputer
{
protected $cpu;
protected $ram;
protected $hdd;
public function __construct()
{
$this->cpu = new Cpu;
$this->ram = new Memory;
$this->hdd = new HardDrive;
}
public function powerOn()
{
$this->ram->load(BOOT_ADDRESS, $this->hdd->read(BOOT_SECTOR, SECTOR_SIZE));
$this->cpu->jump(BOOT_ADDRESS);
$this->cpu->exec();
}
public function powerOff()
{
}
}
$computer = new ComputerFacade;
$computer->powerOn();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment