观察者模式还是助手 class? (php)
Observer pattern or helper class? (php)
想象一下我们的论坛系统,我想post发表评论
class Thread
{
public function post ($userId, $threadId, $comment)
{
SQL INSERT INTO table $userId, $threadId, $comment
// sending emails
// public on a notice-wall
}
}
我不想硬编码 sending emails
和 public on a notice-wall
代码,因为即使它只是两个方法调用,它也会损害 SRP 原则。我可以看到两种方式:
使用助手:
public function post ($userId, $threadId, $comment)
{
SQL INSERT INTO table $userId, $threadId, $comment
ForumHelper::sendEmailsAndPublicOnNoticeWall ($userId, $threadId, $comment);
}
但他们说这是不良做法的标志。
第二,我可以使用观察者模式。那要用什么?
class Thread
{
// ideally make this protected and use setters / getters,
// you could also consider to use an array of listeners
// to have multiple listeners
public $listener = NULL;
public function postComment($userId, $threadId, $comment)
{
// SQL INSERT INTO table $userId, $threadId, $comment
// let's assume that you have a $post object with the
// informations regarding your post
$this->_notifyOfCommentPost($postedComment);
}
protected function _notifyOfCommentPost($postedComment) {
if (!isset($this->listener)) {
return;
}
$this->listener->onPostCommented($postedComment);
}
}
此结构使您能够定义侦听器:
class OnCommentPostedListener {
public function onCommentPosted($postedComment) {
ForumHelper::sendEmailsForComment($postedComment);
ForumHelper::sendPublicOnNoticeWallForComment($postedComment);
}
}
$thread->listener = new OnCommentPostedListener();
这里关于发表评论时要做什么的行为不在管理您的线程的线程 class 中。您的模型(您如何存储信息)不知道您的业务逻辑(发送电子邮件和发布通知)它只是在执行操作时通知外界(Observable 模式)。
这样做的好处是,在发布新评论后添加新行为不需要更改您的线程 class。
想象一下我们的论坛系统,我想post发表评论
class Thread
{
public function post ($userId, $threadId, $comment)
{
SQL INSERT INTO table $userId, $threadId, $comment
// sending emails
// public on a notice-wall
}
}
我不想硬编码 sending emails
和 public on a notice-wall
代码,因为即使它只是两个方法调用,它也会损害 SRP 原则。我可以看到两种方式:
使用助手:
public function post ($userId, $threadId, $comment)
{
SQL INSERT INTO table $userId, $threadId, $comment
ForumHelper::sendEmailsAndPublicOnNoticeWall ($userId, $threadId, $comment);
}
但他们说这是不良做法的标志。 第二,我可以使用观察者模式。那要用什么?
class Thread
{
// ideally make this protected and use setters / getters,
// you could also consider to use an array of listeners
// to have multiple listeners
public $listener = NULL;
public function postComment($userId, $threadId, $comment)
{
// SQL INSERT INTO table $userId, $threadId, $comment
// let's assume that you have a $post object with the
// informations regarding your post
$this->_notifyOfCommentPost($postedComment);
}
protected function _notifyOfCommentPost($postedComment) {
if (!isset($this->listener)) {
return;
}
$this->listener->onPostCommented($postedComment);
}
}
此结构使您能够定义侦听器:
class OnCommentPostedListener {
public function onCommentPosted($postedComment) {
ForumHelper::sendEmailsForComment($postedComment);
ForumHelper::sendPublicOnNoticeWallForComment($postedComment);
}
}
$thread->listener = new OnCommentPostedListener();
这里关于发表评论时要做什么的行为不在管理您的线程的线程 class 中。您的模型(您如何存储信息)不知道您的业务逻辑(发送电子邮件和发布通知)它只是在执行操作时通知外界(Observable 模式)。
这样做的好处是,在发布新评论后添加新行为不需要更改您的线程 class。