Facade design pattern, get attribute from facade
I have something like facade design pattern in my application. We can start this: http://www.pat开发者_StackOverflowternsforphp.org/doku.php?id=facade
From example:
Facade = Computer Parts: CPU, memory...And what is the solution this situation: The computer has an ID. Most of parts need not to know about Computer ID, but there is several parts, which communicate with World, eg. Network card, which need to know Computer ID in which is placed.
What to do - what is the best solution?
Thanks for replies.if i understand you want something like this: You need to send the computerId to the specific part, when you create the specific part and store it private in the object. Like in NetworkDrive. After that you can use the computerId as you 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 to notify the networkDrive object for an eventual changing of computerId
精彩评论