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.发送微信”);

    }

    }

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

  • 相关阅读:
    点滴
    Type.GetType() 返回null的解决办法
    DDD中的实体
    开启博客之路
    Pytorch框架学习(1)张量操作
    GitHub学习之路1
    JavaScript学习之路1
    Java&Eclipse&Maven的折腾
    Ubuntu学习之路1
    Windos下的一些命令集合
  • 原文地址:https://www.cnblogs.com/gzhbk/p/12684320.html
Copyright © 2011-2022 走看看