zoukankan      html  css  js  c++  java
  • guava eventbus demo

    事件总线模式基于发布订阅模式,能够方便将副业务功能从主业务功能中脱离,在主业务逻辑上,只需要post事件,其他逻辑将在事件中执行

    1、使用注解方式

    @Subscribe将监听方法注册到总线上,当总线post event时,所有该event事件的方法都会得到执行

    public class EventBusTest {
    public static void main(String[] args) {
    EventBus eventBus= new EventBus();
    EventAuditor eventAuditor=new EventAuditor( eventBus);
    eventBus.post(new praiseEvent());//子类事件
    eventBus.post(new CommentEvent());//子类事件

    }
    }

    public class EventAuditor   {
    List<BaseEvent> sellEvents = Lists.newArrayList();
    public EventAuditor(EventBus eventBus){
    eventBus.register(this);//该类注册到总线
    }
    @Subscribe
    public void lister(BaseEvent baseEvent) {
    sellEvents.add(baseEvent);
    System.out.printf("%s from %n", baseEvent.praise());
    }
    public List<BaseEvent> getSellEvents() {
    return sellEvents;
    }
    }

    具体事件
    public class CommentEvent implements BaseEvent {
    @Override
    public String praise() {
    return "commentevent";
    }
    }

    2、维护一个列表,用于保存事件监听;每次将事件注册到监听器中,当post相应事件时,通过instanceOf判断具体事件,执行具体事件的逻辑。
    
    
  • 相关阅读:
    jqGrid jqGrid 参数
    jqgrid问题总结
    quartz的配置表达式
    Struts2接收参数的几种方式
    Perl爬虫代码
    PHP官方的PECL扩展有问题
    Perl单URL爬虫
    Perl 多进程进度条
    Perl Tk摸索
    hdu 2058 数学题
  • 原文地址:https://www.cnblogs.com/cindy-zhu/p/7009553.html
Copyright © 2011-2022 走看看