Facade设计模式,从Facade获取属性

发布于 2024-11-18 20:27:41 字数 321 浏览 2 评论 0原文

我的应用程序中有类似外观设计模式的东西。我们可以这样开始: http://www.patternsforphp.org/doku.php?id=facade

来自示例:
外观=计算机
部件:CPU、内存...

这种情况的解决办法是什么:计算机有一个ID。大多数部件不需要知道计算机ID,但有几个部件与世界通信,例如。网卡,需要知道电脑ID放在哪。

该怎么办 - 最好的解决方案是什么?
感谢您的回复。

I have something like facade design pattern in my application. We can start this:
http://www.patternsforphp.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

家住魔仙堡 2024-11-25 20:27:41

如果我明白你想要这样的东西:
当您创建特定部件并将其私有存储在对象中时,您需要将computerId发送到特定部件。就像在网络驱动器中一样。
之后您可以根据需要使用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();

您可以使用观察者模式来通知 networkDrive 对象计算机 ID 的最终更改

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文