观察者
import com.google.common.eventbus.Subscribe;
public class GuavaEvent {
@Subscribe
public void subscribe(String str){
//业务逻辑
System.out.println("执行subscribe方法,传入的参数是:" + str);
}
}
客户端代码
- EventBus 充当被观察者的角色;
- eventbus.register(guavaEvent); 是被观察者对观察者的组合操作;
- eventbus.post("post的内容"); 是被观察者对观察者的广播通知操作;
import com.google.common.eventbus.EventBus;
public class GuavaEventTest {
public static void main(String[] args) {
EventBus eventbus = new EventBus();
GuavaEvent guavaEvent = new GuavaEvent();
eventbus.register(guavaEvent);
eventbus.post("post的内容");
}
}
输出:
执行subscribe方法,传入的参数是:post的内容