观察者可以观察到多个可观察对象吗?

发布于 2024-10-14 18:48:18 字数 167 浏览 6 评论 0原文

试图找到一个这样的例子,有可能我没有采取正确的方法,或者我的思想过度简化了观察者模式的概念。

我想创建一个类来控制来自 Web 服务的消息,并且我希望该类监视许多其他操作的更改。

我见过的观察者模式示例表明许多观察者正在观看单个可观察的内容,我可以(或者应该)反过来做吗?我还应该做什么?

trying to find an example of this, it's possible that I am not going the right way around it, or that my mind has over simplified the concept of the observer pattern.

I want to create a class that controls messages from a web service, and I want this class to monitor the changes of many other operations.

The observer pattern examples I have seen demonstrate many observers watching a single observable, can I (or should I) do this the other way round? What else should I be doing?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

油饼 2024-10-21 18:48:18

只需在许多 Oservable 实例中注册一个 Observer 实例即可。

每当 Observable 更新时,您可能希望将 Observable 实例传递给 Observer,以便 Observable 知道哪个特定对象更新了它。

一个简单的例子:

interface Observer {   
  public update(Observable $observable);
}

class Observable() {
   private $observers = array();

   public function register(Observer $observer) {
     $this->observers[] = $observer;
   }

   public function update() {
     foreach($observers as $observer) {
       $observer->update($this);
     }
   }
}

$observer = new ObserverImplementation();

$observable1->register($observer);
$observable2->register($observer);

$observable1->update();
$observable2->update();

您可能还想查找中介模式

这是它的非常好的实现: Symfony Event Dispatcher

Just register a single Observer instance in many Oservable instances.

You probably will want to pass Observable instance to Observer, whenever Observable updates, so that Observer knows which specific object updated it.

A trivial example:

interface Observer {   
  public update(Observable $observable);
}

class Observable() {
   private $observers = array();

   public function register(Observer $observer) {
     $this->observers[] = $observer;
   }

   public function update() {
     foreach($observers as $observer) {
       $observer->update($this);
     }
   }
}

$observer = new ObserverImplementation();

$observable1->register($observer);
$observable2->register($observer);

$observable1->update();
$observable2->update();

Also you might want to lookup the Mediator pattern.

Here's pretty nice implementation of it: Symfony Event Dispatcher

风吹雪碎 2024-10-21 18:48:18

我认为记住设计模式只是建议而不是绝对非常重要。如果可以对它们进行修改以更好地满足您的需求,那么它们就应该进行修改。

是的,在这种情况下,这是绝对可以做到的。您的观察者只需要注册多个可观察对象即可。

当你的一个可观察对象通知它的观察者时,它只会循环引用列表来告诉他们更新。这些引用是否与其他可观察对象共享并不重要。

I think it's really important to remember that design patterns are suggestions and not absolutes. If they can be modified to better fit your needs then they should.

And yes in this case it can most definitely be done. Your observers will simply need to register with more than one observable object.

When one of your observable objects will notify it's observers it will simply loop on a list of references to tell them to update. It really doesn't matter if these references are shared with other observable objects.

﹎☆浅夏丿初晴 2024-10-21 18:48:18

来自 GoF(观察者模式的实现部分),“观察多个主题。在某些情况下,观察者依赖多个主题可能是有意义的。例如,电子表格可能依赖于在这种情况下,有必要扩展 Update 接口,让观察者知道哪个主体正在发送通知。主体可以简单地将自身作为 Update 操作中的参数传递,从而让观察者知道哪个主体。检查。”

Mchl 的答案已经包含了这个例子。我只是添加 GoF 的参考资料,如果您需要这样做,这将是一个不错的做法。

From GoF (implementation section of the Observer pattern), "Observing more than one subject. It might make sense in some situations for an observer to depend on more than one subject. For example, a spreadsheet may depend on more than one data source. It's necessary to extend the Update interface in such cases to let the observer know which subject is sending the notification. The subject can simply pass itself as a parameter in the Update operation, thereby letting the observer know which subject to examine."

The answer from Mchl already contains the example. I am just adding the reference from GoF that it will not be a bad practice if you need to do this.

听风吹 2024-10-21 18:48:18

我同意 1 个类执行 1 个核心任务的想法。如果是我,我会构建一个观察者接口,创建多个观察者实现,并使用某个 ObserverManager 类管理所有这些观察者。

这样做将分离您所有的业务问题,并提供更精细的测试粒度。

除非“许多其他操作的变化”可以被典型为同一种可观察的“变化”。到那时,单一观察者就有意义了。

I subscribe to the idea that 1 class performs 1 core task. If it were me, I would build an observer interface, create multiple observer implementations, and manage all of those observers with some ObserverManager class.

Doing it this way will separate all of your business concerns, and will afford a finer level of granularity for testing.

Unless the "changes of many other operations" can be typified as the same kind of observable "change." At that point the the single observer makes sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文