JAVA - 发起活动的最佳方式

发布于 2025-01-06 13:43:22 字数 1866 浏览 4 评论 0原文

我有一个关于我的小IRC框架的设计的问题,只是为了学习JAVA事件和观察者。

第一个问题: http://en.wikipedia.org/wiki/Observer_pattern#Critics

观察者模式被批评[6]过于冗长,引入了太多错误并违反了软件工程原则,例如不提倡副作用、封装性、可组合性、概念分离、可扩展性、统一性、抽象、资源管理,语义距离。推荐的方法是逐渐弃用观察者,转而采用响应式编程抽象。

我应该在生产程序中使用观察者模式,如果受到批评并且将来可能会被弃用? java.util.observer 总是一个不错的选择?

第二个问题:我有两个对象。服务器和通道

Server.java

public Server {
    public Server () {
        // Some stuff
        channelList = new ArrayList<Channels>();
    }

    public Channel searchChannel(String channel) {
        // Implements searching channel
    }

    public void parseMessage() {
        if (someCondition1)
           onEvent1();
        elseif (someCondition2)
           onEvent2();
    }

    public void onEvent1(String channel, String param) {
        channel = searchChannel(channel);
        channel.onEvent1(param);
    }

    public void onEvent2(String channel, String param) {
        channel = searchChannel(channel);
        channel.onEvent2(param);
    }
}

** Channel.java**

public Channel {
    public Channel(Server server) {
         // Some stuff
         this.server = server;
    }

    public void onEvent1(String channel, String param) {
        // Stuff for Event1 fired from Server
        server.responseAtEvent("blablabla");
    }

    public void onEvent2(String channel, String param) {
        // Do stuff for Event2 from Server
        server.responseAtEvent("blablabla");
    }

在这种情况下,在通道中我拥有所有公共方法,并且这些方法是从 Server 类调用的... 有更好的方法来管理事件处理吗?我在考虑 ObserverPattern,但我想它更多地用于 GUI 的东西,因此业务逻辑类。

干杯,


我想问另一件事关于观察者...... 为什么很多人不喜欢 JDK 观察者模式并建议实现你的?
为什么要重新发明轮子?

我看到的重新实现的观察者与 JDK 的观察者相同...

I have a question regard design of my little IRC Framework, just for learning JAVA event and observer..

First question:
http://en.wikipedia.org/wiki/Observer_pattern#Critics

The Observer pattern is criticized[6] for being too verbose, introducing too many bugs and violating software engineering principles, such as not promoting side-effects, encapsulation, composability, separation of concepts, scalability, uniformity, abstraction, resource management, semantic distance. The recommended approach is to gradually deprecate observers in favor of reactive programming abstractions.

I should use observer pattern in a production program, if is criticized and maybe deprected in future? java.util.observer is always a good choice?

Second question: I have two objects. Server and Channel

Server.java

public Server {
    public Server () {
        // Some stuff
        channelList = new ArrayList<Channels>();
    }

    public Channel searchChannel(String channel) {
        // Implements searching channel
    }

    public void parseMessage() {
        if (someCondition1)
           onEvent1();
        elseif (someCondition2)
           onEvent2();
    }

    public void onEvent1(String channel, String param) {
        channel = searchChannel(channel);
        channel.onEvent1(param);
    }

    public void onEvent2(String channel, String param) {
        channel = searchChannel(channel);
        channel.onEvent2(param);
    }
}

** Channel.java**

public Channel {
    public Channel(Server server) {
         // Some stuff
         this.server = server;
    }

    public void onEvent1(String channel, String param) {
        // Stuff for Event1 fired from Server
        server.responseAtEvent("blablabla");
    }

    public void onEvent2(String channel, String param) {
        // Do stuff for Event2 from Server
        server.responseAtEvent("blablabla");
    }

In this case, in channel I have all method public and those are called from Server class...
There is a better way to manage event handling? I thinking at ObserverPattern, but I guess it's more used in GUI stuff thus business logic class..

Cheers


I want to ask another thing regard Observer...
Why a lot of people dislike the JDK Observer pattern and suggest to implement your?
Why re-invent the wheel?

The re-implemented observer that I see is same observer of the JDK...

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

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

发布评论

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

评论(2

ゝ杯具 2025-01-13 13:43:22

您不必使用 Java 的观察者接口。
它甚至没有使用泛型。
但是你可以实现你自己的观察者DP。

关于您的服务器/频道
我在其中看到的问题是,扩展它会是一个问题。
如果将来有一个新事件:event3,会发生什么?

我建议您可以在服务器中拥有一个侦听器列表。
一旦有事件发生,一切都会被激活。
每个监听器都可以由一个具体的类来实现。
每个具体类都会检查条件,如果满足,则执行。

You don't have to use the observer interface from Java.
It is not even using generics.
But you can implement your own observer DP.

Regarding your server / channel
The problem I see in it, is that extending it would be a problem.
What happens if you will have a new event: event3 in the future?

I suggest that you can have a list of listeners in the server.
Once an event comes, all are activated.
Each listener can be implemented by a concrete class.
Each concrete class checks the condition, and if satisfies, the it performs.

§普罗旺斯的薰衣草 2025-01-13 13:43:22

在您引用的段落上方,维基百科显示了以下注释:

本文的批评或争议部分可能会损害文章对该主题的中立观点。

因此,这应被视为一种观点,而不是既定事实。

该说法的唯一参考是一篇显然尚未在任何同行评审期刊上发表的论文,该论文中描述的技术也没有得到任何广泛的应用。因此,这只是一个想法。也许这是一个很有前途的想法,但仍然只是一个想法。

另一方面,观察者模式已经被应用了无数次。它的缺陷和局限性已经被彻底探索过,但这些缺陷都不足以成为在所有可能的应用中弃用该模式的充分理由。

维基百科的人们似乎也得出了同样的结论。

因此,如果您想要一个已知的数量,并且不想学习新的编程语言和新的范式来可能改进观察者模式,那么我会将科学探索留给科学家,并坚持经典的观察者模式目前。

Above the paragraph you quoted, Wikipedia displays the following note:

This article's Criticism or Controversy section may compromise the article's neutral point of view of the subject.

So this is to be considered an opinion rather than established fact.

The only reference for that claim is a paper that apparently has not been published in any peer-reviewed journal, nor has the technique described in that paper seen any widespread application. As such, it is just an idea. A promising idea, perhaps, but still just an idea.

The Observer pattern, on the other hand, has been applied countless times. Its defects and limitations have been throughly explored, and none of the those defects is sufficient grounds for deprecating the pattern for all possible applications.

It would appear the people at Wikipedia have come to the same conclusion.

So, if you want a known quantity, and don't want to learn a new programming language and a new paradigm to possibly improve on the observer pattern, I'd leave the scientific exploration to the scientists, and stick to the classic observer pattern for now.

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