zoukankan      html  css  js  c++  java
  • 设计模式(二) --- 使用监听驱动实现观察者模式

    一、实现观察者模式:

      1、继承 ApplicationEvent 类, 定义需要发布的事件类;

      2、实现 ApplicationListener 接口(或者使用 @EventListener)监听事件;

      3、使用 ApplicationEventPublisher 发布定义的事件类

    二、实现

    1、继承 ApplicationEvent 类, 定义需要发布的事件类;

          定义订单事件

    public class OrderEvent extends ApplicationEvent {
        public OrderEvent(Object source) {
            super(source);
        }
    }
    

      

    2、实现 ApplicationListener 接口(或者使用 @EventListener)监听事件;

    2.1、发微信

    // 订单事件的监听器
    @Component // 交给spring托管
    public class WxListener implements ApplicationListener<OrderEvent> {
        @Override
        public void onApplicationEvent(OrderEvent event) {
            // 3 --- 发送微信通知 ----
            System.out.println("3. 发送微信消息");
        }
    }
    

    2.2、发短信

    // 订单事件的监听器
    @Component // 交给spring托管 -- 创建对象并且保留在IOC容器
    public class SmsListener implements ApplicationListener<OrderEvent> {
        @Override
        public void onApplicationEvent(OrderEvent event) {
            // 2 --- 发送短信 --- TODO 此处省略短信接口调用的N行代码
            System.out.println("2、 短信发送成功");
        }
    }
    

    3、定义发布类

    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    import com.jm.listener.event.OrderEvent;
    
    @Component
    public class MyPublisher implements ApplicationContextAware {
    
    	private ApplicationContext applicationContext;
    
    	@Override
    	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    		this.applicationContext = applicationContext;
    	}
    
    	public void publisherEvent(OrderEvent orderEvent) {
    		System.out.println("---开始发布 orderEvent 事件---");
    		applicationContext.publishEvent(orderEvent);
    	}
    
    }
    

      

    测试:

    在 service 中使用

    @Service
    public class TestServiceImpl implements TestService {
    
    	@Autowired
        private MyPublisher myPublisher;
    	
    	@Override
    	public boolean test() {
    
    		System.out.println("============send message test=============");
    		OrderEvent orderEvent = new OrderEvent("参数");
    		myPublisher.publisherEvent(orderEvent);
    		return true;
    	}
    
    }
    

      

    github: https://github.com/szjomin/observerPattern

     ----------------------------------------------------

    参考:https://www.cnblogs.com/jmcui/p/11054756.html

      

  • 相关阅读:
    系统并发报too much open files 错误
    plsql 安装Some Oracle Net versions cannot connect from a path with parentheses
    mysql登录报错
    web.xml配置文件详解之一servlet配置
    hibernate createQuery查询传递参数的两种方式
    mysql登录连接远程数据库命令行
    java 项目打包部署 过程
    flex ArrayCollection的新增与删除的同步
    加大eclipse以及jvm的内存
    2017 八月 UFED Series Releases 系列 6.3 重大更新发布
  • 原文地址:https://www.cnblogs.com/Jomini/p/13047311.html
Copyright © 2011-2022 走看看