设计模式-观察者模式

假设业务:查询快递状态,但不同的快递公司需要不同的参数。(有点离谱但这不重要)

1. 业务层

public interface ExpressService{

    String selectExpressStatusByShunfeng();

    String selectExpressStatusByYuantong();

    String selectExpressStatusByZhongtong();
}
@Service(value = "EventExpressService")
public class ExpressServiceImpl implements ExpressService{
    @Override
    public String selectExpressStatusByShunfeng() {
        SpringUtil.getApplicationContext().publishEvent(
            new ShunfengEvent(this, (byte)1, (float)1.1, Boolean.TRUE));
        return null;
    }

    @Override
    public String selectExpressstatusByYuantong() {
        Springutil.getApplicationContext().publishEvent(new YuantongEvent(
            this, (short)10, 1.11, '\u0031'));
        return null;
    }
    
    @Override
    public String selectExpressstatusByZhongtong() {
        // 懒得做了
        return null;
    }
}

2. Spring容器类

第一种方式

@Component
public class SpringUtil implements ApplicationContextAware {

    /** spring上下文 */
    private static ApplicationContext applicationcontext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throw BeansException {
        SpringUtil.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}

第二种方式

@Component
public class SpringUtil implements ApplicationEventPublisherAware {

    private static ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher application) {
        SpringUtil.applicationEventPublisher = applicationEventPublisher;
    }

    public static ApplicationEventPublisher getApplicationEventPublisher() {
        return applicationEventPublisher;
    }
}

3. 三个快递公司需要的三种实体类

@Data
public class ShunfengEvent extends ApplicationEvent {

    private Byte shunfeng1;

    private Float shunfeng2; 

    private Boolean shunfeng3;

    public ShunfengEvent(Object source,
                         Byte shunfeng1,
                         Float shunfeng2,
                         Boolean shunfeng3){
        super(source);
        this.shunfeng1 = shunfeng1;
        this.shunfeng2 = shunfeng2;
        this.shunfeng3 = shunfeng3;
    }
}
@Data
public class ShunfengEvent extends ApplicationEvent {

    private Short yuantong1;

    private Double yuantong2; 

    private Character yuantong3;

    public YuantongEvent(Object source,
                         Short yuantong1,
                         Double yuantong2,
                         Character yuantong3){
        super(source);
        this.yuantong1 = yuantong1;
        this.yuantong2 = yuantong2;
        this.yuantong3 = yuantong3;
    }
}
// 懒得做了
public class ZhongtongEvent {

    private Integer zhongtong1;

    private BigDecimal zhongtong2;

    private String zhongtong3;
}

4. 三个监听类

@Component
public class ShunfengExpressListener {

    @EventListener
    public void shunfengEvent (ShunfengEvent event) { 
        System.out.println(event.getShunfeng1() + "+" + event.getShunfeng2().toString + "+" + event.getShunfeng3() + "+" + event.getSource().toString());
    }
}
@Component
public class YuantongExpressListener {

    @EventListener
    public void yuantongEvent (YuantongEvent event) { 
        System.out.println(event.getYuantong1() + "+" + event.getYuantong2() + "+" + event.getYuantong3() + "+" + event.getSource());
    }
}
@Component
public class ZhongtongExpressListener {

    @EventListener
    public void zhongtongEvent (ZhongtongEvent event) { 
        System.out.println("继续懒得做,然后因为实体类没继承ApplicationEvent,调用发送时会报空指针异常");
    }
}

5. 测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class EventExpressTest {

    @Autowired
    private ExpressService expressService;

    @Test
    public void test(){
        expressService.selectExpressStatusByShunfeng();
        expressService.selectExpressStatusByYuantong();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值