一个类可以被观察并且成为观察者吗?

发布于 2024-08-02 15:15:17 字数 357 浏览 3 评论 0原文

有一种代码味道,无法决定一个类是否可以成为 Observable 和 Observer,所以我想在这里发帖。

class ObservableAndObserver extends Observable implements Observer {

 // Class is ofcourse registering and receving notifcations on different events
 // effectly actving as a middle man. 

 // Is this a pattern in itself?  'middleman-pattern' ??

}

想法?打破建议零售价?

Had a code smell moment and couldn't decided if its OK for a class to be Observable and also an Observer, so thought I'd post here.

class ObservableAndObserver extends Observable implements Observer {

 // Class is ofcourse registering and receving notifcations on different events
 // effectly actving as a middle man. 

 // Is this a pattern in itself?  'middleman-pattern' ??

}

Thoughts? Breaking SRP?

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

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

发布评论

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

评论(8

巷子口的你 2024-08-09 15:15:17

从技术上讲这是可以的,但您应该检查您是否只是简单地重新传输收到的通知。如果这个类正在观察 class1,然后又被 class2 观察,请尝试让该类观察 class1。

It is technically ok, but you should check that you aren't simply re-transmitting received notifications. If this class is observing class1, and then being observed by class2, try having that class observe class1.

迟到的我 2024-08-09 15:15:17

我看不出有什么理由不这样做。混凝土
不过,例子会有用。更好的是您已经实现的真实代码,我们可以对其进行批评。

I don't see any reason why not. A concrete
example would be useful though. Even better would be real code that you have already implemented, that we could critique.

与他有关 2024-08-09 15:15:17

没关系 :-)。如果类订阅其他类事件并且可以引发自己的事件,那就可以了。当然你可能会做错,但是寻找其他类并可观察的类概念是可以的。

It's ok :-). If class subscribes to other class event and can raise its own events it's ok. Sure you can do it wrong but the concept of class to look for some other class and be observable is ok.

远昼 2024-08-09 15:15:17

虽然我同意尼尔的观点,但我可以看到这是一种代码味道,因为该类可能做了太多事情。

While I agree with Neil, I could see it being a code smell because the class could be doing too much.

翻了热茶 2024-08-09 15:15:17

只要该对象观察其他对象的事件并且它被其他对象观察就可以了。但如果物体观察自己,可能会发现一些非常奇怪的东西。

也许一些代码将有助于理解设计模式是否以奇怪的方式使用。

as long the object observe events of other objects and it is observed by other objects it is ok. But if the object observe itself probably there is something really weird.

Maybe some code will help to understand if the design pattern was used in a weird way.

柠檬色的秋千 2024-08-09 15:15:17

这是许多基于事件的系统实现过滤器的一种方式。在 Java 中,另一种情况会为其中一个角色提供一个内部类,但如果过滤器具有多个具有不同角色的观察者,则这种情况更为常见。因此,如果您认为它最终可能会扮演不同的角色,您可能希望它有一个您当前想到的角色的访问器 getObserver() { return this; } },但这可能是一个 YAGNI,可以稍后添加。

This is one way many event based systems implement filters. The other would, in Java, have an inner class for one of the roles, but that's more common if the filter has multiple observers with different roles. So if you think that it might end up with different roles, you may want it to have an accessor for the role you've currently in mind getObserver() { return this; }, but that is probably a YAGNI and can be added later.

兮颜 2024-08-09 15:15:17

这是邪恶的。
b 观察 a 并通知 c。

所以有一段代码说 a.setValue(...)
c 中发生了一些事情。

现在想一下:
那段代码应该说:
a.setValue(...);
c.doSomething(...);

当然,那段代码现在必须知道 a 和 c 但至少
没有魔法。一切都在阳光下。
oo 中隐藏的信息是针对 setValue(...) 中的代码
观察者/可观察模式中的 no 乘以 2。

干杯,
L。

it's evil.
b observes a and notifies c.

so there is a piece of code that says a.setValue(...)
and something in c happens.

now think this:
that piece of code instead should have said:
a.setValue(...);
c.doSomething(...);

of course that piece of code must now know about a and c but at least
there is no magic. everything is under the sun.
information hiding in oo is for the code in setValue(...)
no in the observer/observable pattern multiplied by 2.

Cheers,
L.

牵强ㄟ 2024-08-09 15:15:17

您可能会误入称为“事件通道”的发布者-订阅者模式的变体,POSA 书中明确引用了该模式:

“在此变体中,创建了一个事件通道并将其放置在发布者与其对于发布者来说,事件通道显示为订阅者,而对于订阅者来说,事件通道显示为发布者”。 (第 341 页)

You might be straying into a variant of the Publisher-Subscriber pattern called Event Channel, which is clearly referenced in the POSA book:

"In this variant, an event channel is created and placed between the publisher and its subscribers. To publishers, the event channel appears as a subscriber, while to subscribers it appears as a publisher". (p. 341)

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