1

アプリケーションにファサードデザインパターンのようなものがあります。これを開始できます: http://www.patternsforphp.org/doku.php?id=facade

例から:
ファサード = コンピュータ
部品: CPU、メモリ...

そして、この状況の解決策は何ですか: コンピュータには ID があります。ほとんどの部分はコンピュータ ID を知る必要はありませんが、World と通信する部分がいくつかあります。配置されているコンピュータ ID を知る必要があるネットワーク カード。

何をすべきか - 最善の解決策は何ですか?
返信ありがとうございます。

4

1 に答える 1

1

あなたがこのようなものが欲しいと私が理解している場合:特定の部分を作成してオブジェクトにプライベートに保存するときに、computerIdを特定の部分に送信する必要があります。NetworkDrive のように。その後、必要に応じて computerId を使用できます。

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

オブザーバー パターンを使用して、computerId の最終的な変更について networkDrive オブジェクトに通知できます。

于 2011-07-04T11:02:09.160 に答える