简体   繁体   English

Java中的MVC和事件监听器

[英]MVC and event listeners in Java

I'm a bit confused on how to use event listeners between the model and controller. 我对如何在模型和控制器之间使用事件监听器感到困惑。 I'm working on an application that on the client side I split in two parts: the Client itself which does all the communication with the server and GUI. 我正在开发一个应用程序,在客户端我分为两部分:客户端本身,它与服务器和GUI进行所有通信。 Client fires events about updates from server and gui fires events about users actions that server is interested in (the app is basically a multiuser graphic editor). 客户端从服务器触发有关更新的事件,并且gui触发有关服务器感兴趣的用户操作的事件(该应用程序基本上是一个多用户图形编辑器)。 So my questions: 所以我的问题:

  1. I found it easier to for GUI to keep a reference to the Client and vice-versa instead of firing events and using event listeners. 我发现GU​​I更容易保持对客户端的引用,反之亦然,而不是触发事件和使用事件侦听器。 Is it generally a bad practice? 这通常是一种不好的做法吗?

  2. Now, if we actually use events, I got confused with the implementation. 现在,如果我们实际使用事件,我会对实现感到困惑。 So I created a few types of events I'll new (drawEvent, connectionEvent, etc..). 所以我创建了一些我将要新的事件(drawEvent,connectionEvent等)。 Then I implemented a ClientListener and a GUIListener with specific behavior. 然后我实现了ClientListener和具有特定行为的GUIListener。 Now, how do I fire events? 现在,我如何解雇事件? From reading, I understood, that in order for GUI to fire something it needs to call clientListener.eventHappened(event), right? 从阅读开始,我明白,为了让GUI能够触发需要调用clientListener.eventHappened(event)的东西,对吧? So it needs to have that listener, ie Client and GUI have to share the listeners (seems to be more complicated that just references)? 所以它需要有那个监听器,即客户端和GUI必须共享监听器(似乎更复杂,只是引用)? And finally, how do I actually add an instance of GUIListener to the gui? 最后,我如何实际向gui添加GUIListener的实例? Again from what I read, I have to make a EventListenerList attribute and just add a new instance of a GUI listener there? 从我读到的内容中,我必须创建一个EventListenerList属性,并在那里添加一个GUI监听器的新实例? Is that it? 是吗? I don't see how this would trigger actual listening. 我不知道这会如何引发实际聆听。

Sorry, I'm just learning all this stuff and don't have a full understanding. 对不起,我只是在学习所有这些东西,并没有完全理解。 Thanks! 谢谢!

Not really sure whether you design is the best, but writing your own listeners/events is rather easy. 不确定你的设计是否最好,但编写自己的听众/事件相当容易。

For example. 例如。 you first define an interface for the listener and a class for the event 首先为侦听器定义接口,为事件定义类

public class MyCustomEvent extends EventObject{
  //store all relevant info in your event
}

public interface MyListener{
  public void eventHappened( MyCustomEvent event);
}

Then, if you have a class to which you want to attach listeners, you just need to have methods to add the listeners, and call the eventHappened method on those listeners when needed 然后,如果你有一个你想要附加监听器的类,你只需要有方法来添加监听器,并在需要时调用那些监听器上的eventHappened方法

public class MyClassWhichFiresEvents{
  private final List<MyListener> listeners = 
    new CopyOnWriteArrayList<MyListener>();

  public void addListener( MyListener listener ){
    listeners.add( listener );
  }

  public void removeListener( MyListener listener ){
    listeners.remove( listener );
  }
  //call this method whenever you need to fire an event
  private void fireEvent( MyCustomEvent event ){
    for ( MyListener listener : listeners ){
      listener.eventHappened( event );
    }
  }
}

If you are wondering about the CopyOnWriteArrayList to store the listeners. 如果您想知道CopyOnWriteArrayList来存储侦听器。 When you are iterating over your listeners to fire the event, there is possibility that one of those listeners removes itself. 当您遍历侦听器以触发事件时,其中一个侦听器可能会自行删除。 So the typical solution is to first copy all the listeners into another list, and iterate over that list, or use a CopyOnWriteArrayList 因此,典型的解决方案是首先将所有侦听器复制到另一个列表中,然后迭代该列表,或使用CopyOnWriteArrayList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM