zoukankan      html  css  js  c++  java
  • java 6大设计原则 一:观察者模式

     =================================

    解耦常用的模式

     =================================

    OrderService.java

    @Service

    public class OrderService{

    @Autowired

    ApplicationContext  applicationContext ;

    public void saveOrder(){

      //1.创建订单

          System.out.println(“1.创建订单”);

         OrderEvent event = new OrderEvent("参数")

          applicationContext.publishEvent(event);

      //2.发送短信

      //System.out.println(“2.发送短信”);

       //3.发送微信(新需求)

      //System.out.println(“3.发送微信”);

    }

    }

    =================================

    orderEvent.java

    //容器启动事件

    public class OrderEvent extends ApplicationEvent{

    public orderEvent(Object source){

    super(source);

    }

    }

    =================================

     SmsListenter.java

    @Component

    public class SmsListenter implements ApplicationListener<OrderEvent >{

     // 有顺序的执行 SmartApplicationListener

    @override

    pulbic ovid onApplicationEvent(OrderEvent orderEvent){

        //2.发送短信

      //System.out.println(“2.发送短信”);

    }

    }

    =================================

    =================================

    新需求: WxListenter.java

    @Component

    public class WxListenter implements ApplicationListener<OrderEvent >{

    @override

    pulbic ovid onApplicationEvent(OrderEvent orderEvent){

        //2.发送微信

      //System.out.println(“2.发送微信”);

    }

    }

    =================================

  • 相关阅读:
    mysql系列:加深对脏读、脏写、可重复读、幻读的理解
    PHP命令空间namespace及use的用法实践总结
    欲望与自制力
    如何被动不费太大力气的提升自己
    经常用到的常识
    Html单选按钮自定义样式
    连连看算法实现 —— 分治实现
    ctime,atime,mtime
    C++实现二叉搜索树的插入,删除
    进程调度算法总结
  • 原文地址:https://www.cnblogs.com/gzhbk/p/12684320.html
Copyright © 2011-2022 走看看