我想在Java中做这样的事情,但我不知道方法:
当事件“object 1 say 'hello'”发生时, 然后对象2通过说“hello”来响应该事件。
有人能给我一个提示或者示例代码吗?
我想在Java中做这样的事情,但我不知道方法:
当事件“object 1 say 'hello'”发生时, 然后对象2通过说“hello”来响应该事件。
有人能给我一个提示或者示例代码吗?
当前回答
您需要的是观察者模式的实现。你可以完全自己做,或者使用java类,如java.util. observer和java.util. observable
其他回答
嗯哼,我最近需要做一些活动,我偶然发现了这个话题。 我决定基于c#版本的事件添加我自己的实现,也许有人会读到它,这对他很有用:
@FunctionalInterface
public interface Action{
void accept(Object... args);
}
public class CustomEvent {
protected List<Action> listeners = new ArrayList<>();
public void addListener(Action arg0){
listeners.add(arg0);
}
public void removeListener(Action arg0){
listeners.remove(arg0);
}
public void invoke(Object... args){
for (Action listener : listeners) {
listener.accept(args);
}
}
}
public class Example1 {
public CustomEvent onValueChanged;
private void doSomething(){
onValueChanged.invoke(); // or .invoke(arg0, arg1, ...)
}
}
public class Example2 {
private Example1 example1;
private Action linkToAction;
private void init(){
example1 = new Example1();
linkToAction = args -> {
doSomethingAnother(); // or doSomethingAnother((Type)args[0], (Type)args[1], ...)
}
example1.onValueChanged.addListener(linkToAction);
}
public void doSomethingAnother(){}
public void unsubscribe(){
example1.onValueChanged.removeListener(linkToAction);
}
}
这是一个简单的例子,Action接口的实现,我做的,是基于Consumer接口的,所以方法名是相似的,但是你也可以改变它。
术语
侦听器是观察者/处理程序 Dispatcher是主题/观察者容器
通常,当人们实现观察者模式时,他们要求调度程序在任何侦听器可以订阅它之前存在。但有一种更好的方法叫做信号。
Signals是一个事件库。它通过引入一个允许注册侦听器和分派事件的Signal对象来解耦分派器的侦听器。信号通过代理从接口自动创建。它负责管理侦听器的所有样板代码,还添加了一些漂亮的糖代码API。
interface Chat{
void onNewMessage(String s);
}
class Foo{
Signal<Chat> chatSignal = Signals.signal(Chat.class);
void bar(){
chatSignal.addListener( s-> Log.d("chat", s) ); // logs all the messaged to Logcat
}
}
class Foo2{
Signal<Chat> chatSignal = Signals.signal(Chat.class);
void bar2(){
chatSignal.dispatcher.onNewMessage("Hello from Foo2"); // dispatches "Hello from Foo2" message to all the listeners
}
}
在本例中,Signal是从Chat界面自动创建的。它允许Foo注册它,并且允许Foo2在没有交互的情况下发送新消息。
免责声明:我是信号的作者。
下面是不完全相同但类似的,我正在搜索一个片段来添加对接口方法的调用,但发现了这个问题,所以我决定为那些像我一样搜索它并发现这个问题的人添加这个片段:
public class MyClass
{
//... class code goes here
public interface DataLoadFinishedListener {
public void onDataLoadFinishedListener(int data_type);
}
private DataLoadFinishedListener m_lDataLoadFinished;
public void setDataLoadFinishedListener(DataLoadFinishedListener dlf){
this.m_lDataLoadFinished = dlf;
}
private void someOtherMethodOfMyClass()
{
m_lDataLoadFinished.onDataLoadFinishedListener(1);
}
}
用法如下:
myClassObj.setDataLoadFinishedListener(new MyClass.DataLoadFinishedListener() {
@Override
public void onDataLoadFinishedListener(int data_type) {
}
});
有3种不同的设置方法:
投球手在接球手内 投球手的接球手 在这个示例测试中的另一个类中的throw和Catcher
默认为选项3,要尝试其他方法,只需取消注释你想成为主类的“可选”代码块,并将该类设置为build.xml文件中的${main - class}变量:
抛出侧代码需要的4件事:
import java.util.*;//import of java.util.event
//Declaration of the event's interface type, OR import of the interface,
//OR declared somewhere else in the package
interface ThrowListener {
public void Catch();
}
/*_____________________________________________________________*/class Thrower {
//list of catchers & corresponding function to add/remove them in the list
List<ThrowListener> listeners = new ArrayList<ThrowListener>();
public void addThrowListener(ThrowListener toAdd){ listeners.add(toAdd); }
//Set of functions that Throw Events.
public void Throw(){ for (ThrowListener hl : listeners) hl.Catch();
System.out.println("Something thrown");
}
////Optional: 2 things to send events to a class that is a member of the current class
. . . go to github link to see this code . . .
}
类文件中接收类事件所需的东西
/*_______________________________________________________________*/class Catcher
implements ThrowListener {//implement added to class
//Set of @Override functions that Catch Events
@Override public void Catch() {
System.out.println("I caught something!!");
}
////Optional: 2 things to receive events from a class that is a member of the current class
. . . go to github link to see this code . . .
}
您需要的是观察者模式的实现。你可以完全自己做,或者使用java类,如java.util. observer和java.util. observable