最佳实践 - 将事件声明为Java接口的一部分 [英] Best practice - Declaring an event as part of a Java interface

查看:192
本文介绍了最佳实践 - 将事件声明为Java接口的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用接口和事件来解耦一些UI代码,我想知道Java中是否有方法/最佳实践,用于将事件声明为Java接口的一部分,就像C#提供的一样: / p>

  //接口
中的C#事件声明public interface IAction
{
事件EventHandler OnAction;
}

我的目标只是标记一个界面,以便它被知道(实现者)触发特定类型的事件。我希望我可以包括更多的文档来强制执行这种行为。我意识到Java不提供事件关键字或直接的方法,所以我希望有一些解决方法或最佳实践的建议。



我可以想到这样做的一个方法是创建一个通用的标记界面来代表触发事件的能力:

  public接口FiresEvent< T extends Event> {
public void fireEvent();
}

...然后在我的自定义界面中继承该界面。 p>

  public interface MyInterface extends FiresEvent< ActionEvent> {

}

这种方法的问题是FiresEvent只有继承一次,即使泛型类型发生变化,所以解决方案看起来似乎并不足够,对于一个对象可能是多个事件的来源。



I要知道人们会如何处理这个问题,如果有更好的方法,只需要记录是否需要点燃事件。



编辑: / strong>
以下内容可能会清除我的问题:



我理解Java处理程序列表方法和自我处理,允许调用者注册处理程序目的。在我的情况下,我依靠一个事件巴士,所以我们可以看到触发事件,把它们放在那里,还有一些事情要重定向到处理程序。



我想定义接口的责任:




  • 界面实现者将T类事件触发到世界/事件总线/任何东西

  • 接口实现者不必委托给注册的侦听器


解决方案

Java处理的事件与以前有些不同,但是这个概念是一样的。你创建一个所谓的事件侦听器,当事件发生时它被调用。这可以是更好的结构,因为它允许多个监听器被注册。



我建议您浏览这个教程


I'm attempting to decouple some UI code using interfaces and events, and I would like to know if there is way/best practice in Java for declaring an event as part of a Java interface - something like what C# provides:

// C# event declaration in interface
public interface IAction
{
    event EventHandler OnAction;
}

My goal is simply to mark an interface so that it is known it (implementors) fires events of a particular type. I'm hoping I can include more than documentation only to enforce that behaviour. I realize Java does not provide the "event" keyword or a direct way of doing this, so I'm hoping for some advice on a workaround or best practice on achieving this.

One way I can think of doing this is by creating a generic marker interface that represents the capability to fire events:

public interface FiresEvent<T extends Event> {
    public void fireEvent();
}

... and then to inherit that interface in my custom interface.

public interface MyInterface extends FiresEvent<ActionEvent> {

}

The problem with this approach is that "FiresEvent" can only be inherited once, even if the generic type changes, so the solution does not seem generic enough for the case where an object may be the source of multiple events.

I'd be curious to know how people would handle this, and if there is a better way than to just document the need to fire events.

EDIT: Maybe the following will clear up my question:

I understand the Java handler list approach and self-handling by allowing callers to register handlers against an object. In my case I am relying on an event bus, so we can view "firing" events as putting them out there, for something else to redirect to handlers.

I'd like to define the interface responsibility as:

  • The interface implementer fires an event of type T into the world/event bus/anything
  • The interface implementer does not have to delegate to registered listeners itself

解决方案

Java handles events a bit differently than what you're used to, however the concept is the same. You create what's called an event listener, and it's called when the event happens. This can be a better construct as it allows for multiple listeners to be registered.

I suggest browsing this tutorial

这篇关于最佳实践 - 将事件声明为Java接口的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆