在工厂上实施观察者模式是否合理?

发布于 2025-01-09 18:14:37 字数 1380 浏览 5 评论 0原文

我创建了一个名为 duckFactory 的工厂,它从一些 Duck 类创建不同的 Duck,现在我希望每次创建鸭子时都必须通知 duckStore 类。为此,我创建了一个 DuckFactoryObserver 接口,并在 duckFactory 类上实现了该接口。

这是我创建的观察者界面。

namespace App\interfaces\Observers;

interface DuckFactoryObserver {
    public function add( Duck $duck );

    public function notify();
}

这就是我实现观察者模式的地方。

namespace App\Factories;
use App\Characters\MallardDuck;
use App\Characters\RubberDuck;
use App\Characters\DecoyDuck;
use App\interfaces\Observers\DuckFactoryObserver;

class DuckFactory implements DuckFactoryObserver {

    private $ducks = [];

    public function duckCreator( string $color, string $duckType ) {

        switch ( $duckType ) {
            case 'mallard':
            return new MallardDuck( $color, $duckType );
            break;
            case 'rubber':
            return new RubberDuck( $color, $duckType );
            break;
            case 'decoy':
            return new DecoyDuck( $color, $duckType );
            break;
            default:
            return 'No Duck of Such Type';
            break;
        }
    }

    public function add( Duck $duck ) {
        $this->ducks[] = $duck;
    }

    public function notify() {
        foreach ( $this->ducks as $duck ) {
            echo $duck->display();
        }
    }
}

我的疑问是,在另一种模式上实施一种模式是明智的选择吗?

I have created a Factory by the name duckFactory which creates different Ducks from some Duck classes, now I want every time a duck is created the duckStore class must be notified. For this purpose, I created a DuckFactoryObserver interface which I implemented on the duckFactory class.

This is the Observer interface I created.

namespace App\interfaces\Observers;

interface DuckFactoryObserver {
    public function add( Duck $duck );

    public function notify();
}

This is where I Implemented the Observer pattern.

namespace App\Factories;
use App\Characters\MallardDuck;
use App\Characters\RubberDuck;
use App\Characters\DecoyDuck;
use App\interfaces\Observers\DuckFactoryObserver;

class DuckFactory implements DuckFactoryObserver {

    private $ducks = [];

    public function duckCreator( string $color, string $duckType ) {

        switch ( $duckType ) {
            case 'mallard':
            return new MallardDuck( $color, $duckType );
            break;
            case 'rubber':
            return new RubberDuck( $color, $duckType );
            break;
            case 'decoy':
            return new DecoyDuck( $color, $duckType );
            break;
            default:
            return 'No Duck of Such Type';
            break;
        }
    }

    public function add( Duck $duck ) {
        $this->ducks[] = $duck;
    }

    public function notify() {
        foreach ( $this->ducks as $duck ) {
            echo $duck->display();
        }
    }
}

My doubt is, is it a wise choice to implement one pattern on the other?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文