我是android (和java)的新手,我想知道对象之间的通信机制是什么?例如,如果我想监控一个私有成员的值,如果它发生了变化,我希望触发一个方法。
我知道对于UI事件,有事件侦听器。但是如果不是鼠标点击,而是一个私有成员的值发生了变化呢?我尝试了intent/broadcastreceiver,但我不认为这是我想要做的。
非常感谢!
发布于 2011-08-12 04:45:05
您可以在这里使用基于发布者和订阅者的方法。包含私有(Publisher)成员的类将有一个方法RegisterForChange该方法,您将接受一些接口ISomeInterface作为参数,Subscriber类将实现该接口。私有成员发生更改后,您可以调用该接口的SomeMethod方法来通知Subscriber我的私有成员已更改
//pseudocode
public interface ISomeInterface
{
SomeMethod();
}
class Publisher
{
RegisterForChange(ISomeInterface inter)
{
inter.SomeMEthod();
}
//whenever there is change call
inter.SomeMethod();
}
class Subsscriber implements ISomeInterface
{
//inside this class register for change by calling
Publisher.RegsiterForChange(this);
// this method will be called whenever there is change in private member
public SomeMethod()
{
}
}https://stackoverflow.com/questions/7032305
复制相似问题