记录常用设计模式
策略模式+工厂模式
1.四种玩法,玩法出入参类型一致,根据类型区分具体玩法。
/** * 玩法枚举类 * * @author jiangym * @version 1.0 * @date 2022/9/28 20:10 */ public enum TricksEnum { /** * vip身份用户 */ VIP_USER(1, "vip身份用户"), /** * 全站用户 */ USER_ALL(2, "全站用户"), /** * 历史充值累充用户 */ HISTORY_CHARGE_USER(3, "历史充值累充用户"), /** * 充值活动白名单用户 */ ACTIVITY_WHITE_USER(4, "充值活动白名单用户"), ; /** * 序号 */ private final int code; /** * 描述 */ private final String msg; public String msg() { return msg; } public int code() { return code; } TricksEnum(int code, String msg) { this.code = code; this.msg = msg; } }
/** * 玩法工厂类 * @author jiangym * @version 1.0 * @date 2022/8/16 10:00 */ public class TricksHandlerFactory { /** * HashMap,存放AppName,具体handler */ private static final HashMap<Integer, TricksStrategy> strategyMap = Maps.newHashMap(); /** * 通过对象获取对应的策略类 * @param type * @return */ public static TricksStrategy getInvokeStrategy(Integer type) { return strategyMap.get(type); } /** * 将策略类put进HashMap * @param type * @param handler */ public static void register(Integer type, TricksStrategy handler) { if (type == null || type <= 0 || handler == null) { return; } strategyMap.put(type,handler); } }
/** * 玩法 策略模式 现有玩法:白名单、全站、VIP、历史累充 * 策略模式+工厂模式 * @author jiangym * @version 1.0 * @date 2022/8/19 11:19 */ public interface TricksStrategy extends InitializingBean { /** * 处理四种玩法的充值消息 * @param uid * @param realAmount * @param participateUserConfigDto */ void tricksChargeLogic(Long uid, Integer realAmount, ParticipateUserConfigDto participateUserConfigDto, UcParamVO userUcParamVO); /** * 校验四种玩法是否可以参与 当前用户 * @param uid * @param participateUserConfigDto * @param appName * @return */ String tricksCheckUserCanParticipate(Long uid, ParticipateUserConfigDto participateUserConfigDto, String appName); }
/** * @author jiangym * @version 1.0 * @date 2022/9/28 20:04 */ @Slf4j @Component public class ActivityWhiteUserHandler implements TricksStrategy { @Resource private ChargeGiftService chargeGiftService; @Override public void afterPropertiesSet() { TricksHandlerFactory.register(TricksEnum.ACTIVITY_WHITE_USER.code(),this); } @Override public void tricksChargeLogic(Long uid, Integer realAmount, ParticipateUserConfigDto participateUserConfigDto, UcParamVO userUcParamVO) { chargeGiftService.dealWhiteChargeLogic(uid, realAmount, participateUserConfigDto, userUcParamVO); } @Override public String tricksCheckUserCanParticipate(Long uid, ParticipateUserConfigDto participateUserConfigDto, String appName) { return chargeGiftService.checkWhiteParticipate(uid, participateUserConfigDto, appName); } }
/** * 遍历马甲包的配置里的每个配置 判断是否 当前充值逻辑 可以参与活动 * @param uid * @param userUcParamVO * @param realAmount * @param participateUserConfigId */ private void ergodicConfigDealCharge(Long uid, UcParamVO userUcParamVO, Integer realAmount, int participateUserConfigId) { List<ParticipateUserConfigDto> participateUserConfigDtoList = getParticipateUser(participateUserConfigId); for (ParticipateUserConfigDto participateUserConfigDto : participateUserConfigDtoList) { if (participateUserConfigDto.getIsJoin() == null ||participateUserConfigDto.getIsJoin() != PARTICIPATE) { continue; } TricksStrategy tricksStrategy = TricksHandlerFactory.getInvokeStrategy(participateUserConfigDto.getIdType()); if (tricksStrategy != null) { tricksStrategy.tricksChargeLogic(uid, realAmount, participateUserConfigDto, userUcParamVO); } } }
装饰器模式
抽象类 ABattercake
具体实现类 Battercake继承 抽象类
装饰类 加鸡蛋加肠继承 抽象类的具体类 AbstractDecorator
装饰的类通过 父类里声明的batterCake , 在其方法里添加具体的装饰。
模板模式
参考: