Java应用程序间表单通信观察者模式

发布于 2024-08-31 06:32:08 字数 860 浏览 2 评论 0原文

观察者模式?我在哪里可以得到 Java 中的示例(我知道谷歌,但也希望获得一些个人见解。)

关于我的问题的正确解释:

我有 3 个表单/窗口。 “board”是作为应用程序加载的主要形式。

“聊天”是文本聊天发生的地方。

“网络”是建立网络连接的地方。

我有游戏(connect4)在本地运行,我也想实现它的网络版本。

我的想法是,它可能与观察者模式有关,有一个线程(或其他东西)在运行时监视网络状态并更新当前网络状态的聊天和面板形式以及传递从网络接收到的数据。

我的想法有效吗?或者我应该如何在整个应用程序中建立网络和网络状态更新?

感谢您的意见。

公告板 http://img39.imageshack.us/img39/5221/boardz.jpg

聊天 http://img691.imageshack.us/img691/3629/chatos.jpg

网络 http://img441.imageshack.us/img441/5906/networks .jpg

编辑:有没有人可以推荐一本关于 Java 观察者模式的书?

Observer pattern? Where do i get examples of this in Java (i know google but was hoping for some personal insight also.)

On to a proper explanation of my issue:

i have 3 forms/windows. "board" is the main form that loads as the application.

"chat" is where the text chat takes place.

"network" is where network connection is established.

i have the game (connect4) working locally and i would like to implement a networked version of it also.

my idea is maybe it is related to Observer pattern to have a thread (or something) monitoring network state during runtime and update the chat and board forms of the current network status as well as delivering received data from the network.

are my ideas valid? or how should i go about establishing network and network status updates throughout the application?

thank you for your input.

board http://img39.imageshack.us/img39/5221/boardz.jpg

chat http://img691.imageshack.us/img691/3629/chatos.jpg

network http://img441.imageshack.us/img441/5906/networks.jpg

EDIT: is there a book on Java Observer pattern any one can recommend?

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

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

发布评论

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

评论(3

痞味浪人 2024-09-07 06:32:09

我发现这对于从 Java 角度学习 GOF 设计模式非常有帮助。我发现它比 GOF 书更容易理解,因为我是一名 Java 开发人员。

http://www.amazon.com/Applied-Java-Patterns-Stephen-Stelting/ dp/0130935387

I have found this one very helpful for learning GOF design patterns from a Java angle. I found it more accessible than the GOF book since I'm a java developer.

http://www.amazon.com/Applied-Java-Patterns-Stephen-Stelting/dp/0130935387

温馨耳语 2024-09-07 06:32:08

观察者模式在 Java 中通常被称为事件监听器。您可能也想搜索它。

您的想法似乎是有效的,尽管您有时需要深入研究更多技术细节。您是否使用 RMI、Caucho、HTTPInvoker 来实现网络通信?

无论如何,您需要游戏“服务器”能够向所有玩家发布更新。这可以使用轮询或通过网络传递对象来实现。

StackOverflow 上有一些很好的参考资料 - 检查这个一个还有一个。这应该能让你继续前进。

The observer pattern is often referred to as event listeners in Java. You might want to search for that as well.

Your idea seems valid, although you will need to delve in more technical details at some point. Are you implementing your network communication using RMI, Caucho, HTTPInvoker?

Anyhow you need the game "server" to be able to publish updates to all players. This can be implemented using polling or by passing an object over the network.

There are some good references right here on StackOverflow - check this one and that one also. That should get you going.

断念 2024-09-07 06:32:08

在这种情况下,聊天和游戏窗口将被视为观察者。您需要实现一个名为 Observers 之类的接口(java API 在 java.util.Observer 中有一个本机观察者模式,但您可以实现自己的),并让您的聊天和看板窗口实现它们。您可能会认为它是这样的:

public interface Observer 
{
public void postUpdate(String newData); 
}

postUpdate 是所有观察者都需要定义的函数,并且这些类和您以后想要包含的任何其他内容都需要实现观察者,例如:

public class ChatWindow Implements Observer
{
//bla bla bla
}

然后,在您的网络类中,您'然后

public void addObserver(Observer newObserver)
{
//Do something here to add the new Observer to some list of Observers, maybe a
//List<Observer> or something?
}

,在初始化期间,您需要从每个您想要监视的观察者中调用 addObserver 函数,并在网络类中有一些逻辑来通过调用其 postUpdate 函数来更新已注册的每个人。在这种情况下,我的行为就好像您想向他们发送一个包含新数据的字符串,这只是一个选项。您也可以让它不传递任何内容,仅充当已发生更新的通知,在这种情况下,观察者将负责检查他们关心的来自网络的数据是否已更改;或者也许您可以让它传递一些表示网络上已更新内容的数据,以便该类可以知道它是否关心已更改的内容(例如,游戏板可能不关心聊天数据是否发生了唯一的更改) ,所以不会打扰做其他事情)。

您可能会在书籍方面找到最成功的研究 Head First 设计模式的方法。

In this instance the chat and game windows would be considered Observers. You'll want to implement an interface called something like Observers (the java API has a native Observer pattern at java.util.Observer but you can implement your own) and have your chat and board window implement them. You might have it look like:

public interface Observer 
{
public void postUpdate(String newData); 
}

postUpdate is a function that all of the Observers will need to define, and both these classes and anything else you want to include later will need to implement Observer, like:

public class ChatWindow Implements Observer
{
//bla bla bla
}

Then, in your network class, you'll want a method like

public void addObserver(Observer newObserver)
{
//Do something here to add the new Observer to some list of Observers, maybe a
//List<Observer> or something?
}

Then, during initialization, you will want to want to call the addObserver function from each Observer you want to have watching it, and have some logic in your network class to update everybody who has registered by calling their postUpdate functions. In this case I acted as though you want to send them a String containing new data, that is only one option. You could also have it pass nothing and just act as notification that an update has occured in which case the Observers will be responsible for checking if the data they care about from the network has changed; or maybe you could have it pass some data representing what exactly has been updated on the network, such that the class could know if it cares about what has changed (for example, the gameboard might not care if the only change has occured to chat data, so it won't bother doing anything else).

You may find best success investigating Head First Design Patterns, in terms of books.

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