温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

观察者模式+AOP 代码示例

发布时间:2020-06-30 11:07:58 来源:网络 阅读:787 作者:乾坤刀 栏目:软件技术

背景

当经纪人创建客户时,需要给对应的经纪人增加战报信息。在代码层面上,客源的相关类只针对客源数据表操作。而战报信息包含了多种业务统计数据,客源只是其中统计的部分数据。鉴于两者相对独立,且客源的战报信息会有所修改。因此,采用AOP+观察者模式构建代码。

代码

定义一个注解,用于AOP拦截。

/**
 * 战报注解
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.PARAMETER})
@Documented
public @interface AchievementAnnotation {

    OperateEnum operate() default OperateEnum.ADD;

    enum OperateEnum{
        ADD,UPDATE,DELETE
    }
}

定义AOP,用户获取数据,并转发给观察者

/**
 * 战报AOP
 */
@Aspect
@Component
public class AchievementAop {

    /**
     * 战报观察者列表
     */
    private List<AchievementObserver> observerList;

    public AchievementAop() {
        this.observerList = new ArrayList<>();
    }

    public List<AchievementObserver> getObserverList() {
        return observerList;
    }

    public void setObserverList(List<AchievementObserver> observerList) {
        if (null != this.observerList)
            this.observerList.addAll(observerList);
        this.observerList = observerList;
    }

    /**
        *注入客源的观察者
        */
    @Autowired
    public void setCustomerAchievementObserver(CustomerAchievementObserver customerAchievementObserver) {
        getObserverList().add(customerAchievementObserver);
    }

    @Pointcut("@annotation(com.pretang.cloud.aop.AchievementAnnotation)")
    private void pointCut() {
    }

    @AfterReturning(pointcut = "pointCut()", returning = "retVal")
    public void after(JoinPoint joinPoint, Object retVal) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method targetMethod = methodSignature.getMethod();
        AchievementAnnotation annotation = targetMethod.getAnnotation(AchievementAnnotation.class);
        AchievementAnnotation.OperateEnum operateEnum = annotation.operate();
        for (AchievementObserver observer : observerList) {
            if (observer.isSupport(retVal))
                observer.execute(retVal);
        }
    }
}

定义观察者通用接口

/**
 * 战报信息观察者接口
 * @param <T>
 */
public interface AchievementObserver<T> {

    /**
     * 是否支持该对象
     * @param obj
     * @return
     */
    boolean isSupport(Object obj);

    /**
     * 操作业务数据
     * @param t
     * @throws RuntimeException
     */
    void execute(T t) throws RuntimeException;
}

客源观察者

/**
 * 客源信息的观察者
 */
@Component
public class CustomerAchievementObserver implements AchievementObserver<CustomerBase> {

    @Autowired
    private CustomerRpcService customerRpcService;

    @Override
    public boolean isSupport(Object obj) {
        return obj instanceof CustomerBase;
    }

    @Override
    public void execute(CustomerBase customerBase) throws RuntimeException {
            // 实际业务处理
        customerRpcService.saveAchievement(customerBase.getAgentUserId(), "ADD_CUSTOMER", customerBase.getId());
    }
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI