Android中的Handler是用来在类之间发送消息的。例如:
public class Foo
{
private Handler handler;
public Foo(Handler handler)
{
this.handler = handler;
// ...
}
public void fooMethod()
{
// ...
handler.obtainMessage("blah blah").sendToTarget();
}
}
主要:
public class Main
{
private Handler handler;
public Main()
{
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
// handle the message that is sent from Foo
}
};
Foo f = new Foo(handler);
// ...
}
}
那么,我正在寻找 Java 中的类似技术,还是应该实现自己的处理程序?
最佳答案
我相信您正在寻找像观察者模式这样的东西,它可以使一个对象与另一个对象的通信变得容易。
参见>>> http://www.javaworld.com/javaqa/2001-05/04-qa-0525-observer.html
还有>>> http://www.oodesign.com/observer-pattern.html
我希望我能帮到你......
关于java - Java中有没有类似于Android中的android.os.Handler的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9558779/