有news
和newsTranslations
。每次创建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));
你怎么看?这是一种更好的方法吗?因为我看到很多例子,但我看不到这种分离