观察者模式的用法:update()方法
您好,我们(我的朋友)有不同的来源(wiki 内容、论坛评论、文档上传、RSS 来源...),我们希望在未来的系统中以不同的方式通知这些来源:Javascript LIVE PUSH (APE)、邮件、SMS 、电话、RSS...
我们为我们的实现考虑 GoF Observer 设计模式。 观察者设计模式是实现这一目标的好方法吗?
我们考虑让 Observer 放置代码来格式化要通知的消息(参见示例 1),但也许我们应该向 Observable 返回状态以创建消息(参见示例 2)。 就模式而言,这是一个好的做法吗?
如果是, update() (默认观察者设计模式方法)的“真正作用”是什么?我们真的不明白。
我们在网上找到了这个示例(用于 update() 实现)
1) http://gbenoit79.blogspot.com/2011/04/design-pattern -observer.html
<?php
(...)
class ConcreteObserverA implements SplObserver
{
public function update(SplSubject $subject)
{
echo 'ConcreteObserverA received: ' . $subject->getVariable() . "\n";
}
}
(...)
2) http://www.minte9.com/kb /php-observer-pattern-example-php-programming-advanced-i1365
<?php
(...)
class DriverObserver implements Observer{
public function update(Observable $subject) {
if ($subject->driven_hours >= 8) {
$subject->need_sleep = 1;
} else {
$subject->need_sleep = 0;
}
}
}
(...)
它们都有效吗?
Hi We (a friend an I) have different sources (wikis contents, forums comments, document uploads, RSS sources...) that we want to notify in our future system in different outs : Javascript LIVE PUSH (APE), mail, SMS, telephone, RSS...
We think about GoF Observer design pattern for our implementation.
Is the Observer design pattern a good way to achieve this ?
We think about Observer to put code to format the message to be notified(cf example 1), but maybe we shall return a status to the Observable in order to create the message(cf example 2).
Is it a good practice in regard of the pattern ?
if yes, what is the "real role" of update() (default Observer design pattern method) ? We don't really get it.
We have found this examples on the web (for update() implementation)
1)
http://gbenoit79.blogspot.com/2011/04/design-pattern-observer.html
<?php
(...)
class ConcreteObserverA implements SplObserver
{
public function update(SplSubject $subject)
{
echo 'ConcreteObserverA received: ' . $subject->getVariable() . "\n";
}
}
(...)
2)
http://www.minte9.com/kb/php-observer-pattern-example-php-programming-advanced-i1365
<?php
(...)
class DriverObserver implements Observer{
public function update(Observable $subject) {
if ($subject->driven_hours >= 8) {
$subject->need_sleep = 1;
} else {
$subject->need_sleep = 0;
}
}
}
(...)
Are they both valids ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有更多细节,很难说观察者模式是否是一个好的解决方案,但它可能是一个有效的解决方案。
观察者是观察其他对象的对象,这些对象称为主体。当某个主题发生变化时,它会通知其观察者其状态的更新。
以你的例子:
您的主题是 wiki 内容、论坛评论、文档上传、RSS 源...而您的观察者是 Javascript LIVE PUSH (APE)、邮件、SMS、电话、RSS...
您应该为主题编写一个抽象类,该抽象类应该包含观察者对象的集合,具有添加到该集合、从中删除的方法以及一些选择要通知的观察者的方法。 Observable 的具体实现将使用一些消息数据作为参数来调用所选观察者的更新。您需要一个观察者接口,该接口将具有更新方法,当主题更新时会调用该方法。具体的实现将是在该更新方法中发送电子邮件、推送到网络或注销自己,等等。
编码完成后,您可以通过创建观察者、主题并向主题添加观察者来初始化通信。
Without more details, it is not easy to say if Observer pattern is a good solution, however it might be a valid one.
Observer is an object observing other objects, which are called subjects. When a change happens in some subject, it notifies its observers about the update of its state.
To your example:
Your subjects are wikis contents, forums comments, document uploads, RSS sources... and your observers are Javascript LIVE PUSH (APE), mail, SMS, telephone, RSS...
You should write an abstract class for subjects that should hold collection of Observer objects, ahve methods to add to that collection, remove from it and some methods to choose which observers to notify. Concrete implementations of Observable would than call update on chosen observers with some message data as arguments. Than you need an Observer interface which will have update method which is called when subjects get updated. Concrete implementations would than in that update method send e-mails, push to the web or unregister themselves, whatever.
After this is coded, you initialize the communication by creating Observers, than subjects and adding observers to subjects.