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

}

}

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

 

posted @ 2020-04-12 11:26  A汉克先生  阅读(151)  评论(0编辑  收藏  举报