Java设计模式-工厂

ModuleExcutorFactory

package com.cloud.gcm.excutor;

import com.cloud.gcm.config.ModuleAnnotation;
import com.cloud.gcm.service.common.ModuleConvert;
import com.cloud.gcm.util.AopTargetUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @description: 转换器工厂
 * @author: wangzhixiang
 * @create: 2020-10-27 20:19
 **/
@Component
public class ModuleExcutorFactory implements ApplicationContextAware {
    private static Map<String, ModuleConvert> executors = new HashMap<>();

    @SuppressWarnings({"unchecked", "rawtypes"})
    public static <T extends ModuleConvert> T getExecutor(String module) {
        return (T)executors.get(module);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        if(executors.size() > 0 ) {
            return;
        }

        Map<String, Object> beans = applicationContext.getBeansWithAnnotation(ModuleAnnotation.class);
        if(beans!=null && !beans.isEmpty()) {

            Iterator<Map.Entry<String, Object>> iterator = beans.entrySet().iterator();
            while(iterator.hasNext()) {

                Map.Entry<String, Object> entry = iterator.next();

                if(entry.getValue() instanceof ModuleConvert) {
                    @SuppressWarnings("rawtypes")
                    ModuleConvert bean =  (ModuleConvert)entry.getValue();

                    ModuleAnnotation instanceInfo = null;
                    try {
                        instanceInfo = AopTargetUtils.getTarget(bean).getClass().getAnnotation(ModuleAnnotation.class);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    String key = instanceInfo.module();

                    executors.put(key, bean);
                }
            }
        }
    }
}

ModuleConvert

public R updateUserStatus(UserStatusRecordEntity entity, Integer scene) {
    //throw new RRException(ErrorCode.SYS_ERROR);
    return R.success();
}

BHOSTModuleConvert

package com.cloud.gcm.service.impl.common;

import com.cloud.gcm.cloudapi.bhost.BHostReqDTO;
import com.cloud.gcm.config.ModuleAnnotation;
import com.cloud.gcm.config.R;
import com.cloud.gcm.entity.api.UserStatusRecordEntity;
import com.cloud.gcm.mapper.api.UserStatusRecordMapper;
import com.cloud.gcm.service.common.ModuleConvert;
import com.cloud.gcm.service.common.TaskLogService;
import com.cloud.gcm.service.fortress.FortressApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@ModuleAnnotation(module = "BHOST")
@Component
public class BHOSTModuleConvert extends ModuleConvert {
    @Autowired
    FortressApiService fortressApiService;
    @Autowired
    UserStatusRecordMapper userStatusRecordMapper;
    @Autowired
    TaskLogService taskLogService;

    @Override
    public R updateUserStatus(UserStatusRecordEntity item, Integer scene) {
        R result = new R();
        return result;
    }
}

调用代码:

ModuleExcutorFactory.getExecutor(item.getModule()).updateUserStatus(item, 1);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值