php - Facade design pattern, get attribute from facade -
i have facade design pattern in application. can start this: http://www.patternsforphp.org/doku.php?id=facade
from example:
facade = computer
parts: cpu, memory...
and solution situation: computer has id. of parts need not know computer id, there several parts, communicate world, eg. network card, need know computer id in placed.
what - best solution?
replies.
if understand want this: need send computerid specific part, when create specific part , store private in object. in networkdrive. after can use computerid want.
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 ) { /* ... */ } } class networkdrive { private $computerid; public function __construct($id) { $this->computerid = $id; } public function send() { echo $this->computerid; } } /* facade */ class computer { protected $cpu = null; protected $memory = null; protected $harddrive = null; protected $networkdrive = null; private $id = 534; public function __construct() { $this->cpu = new cpu(); $this->memory = new memory(); $this->harddrive = new harddrive(); $this->networkdrive = new networkdrive($this->id); } 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(); $this->networkdrive->send(); } } /* client */ $facade = new computer(); $facade->startcomputer(); you can use observer pattern notify networkdrive object eventual changing of computerid
Comments
Post a Comment