你是否认为从观察员那里再做一个课程是值得的?#34;宾语?

时间:2015-10-03 08:45:51

标签: php observer-pattern

newsnewsTranslations。每次创建new时,至少应存在一个translation

class NewsModel
{
    public function add()
    {
        $newId = SQL INSERT INTO NEWS
        $this->notifyAdd ($newId);
    }
}

class NewsTranslationModel
{
    public function add ($newId)
    {
        SQL INSERT INTO
    }

    public function notifyAdd($newId)
    {
        $this->add ($newId);
    }
}

$news = new NewsModel();
$newsTranslation = new NewsTranslationModel();
$news->attach ($newsTranslation);

但是就这样,我觉得NewsTranslationModel有太多的责任。如果我写这个怎么办:

class NewsModel
{
    public function add()
    {
        $newId = SQL INSERT INTO NEWS
        $this->notifyAdd ($newId);
    }
}

class NewsTranslationModel
{
    public function add ($newId)
    {
        SQL INSERT INTO
    }
}

class NewsTranslationObserver
{
    private $newsTranslation;

    public function __constructor ($newsTranslation)
    {
         $this->newsTranslation = $newsTranslation;
    }

    public function notifyAdd($newId)
    {
        $this->newsTranslation->add ($newId);
    }
}

$news = new NewsModel();
$newsTranslation = new NewsTranslationModel();
$news->attach (new NewsTranslationObserver($newsTranslation));
你怎么看?这是一种更好的方法吗?因为我看到很多例子,但我看不到这种分离

0 个答案:

没有答案