Java设计模式项目使用实例--TapJoy之策略模式

本文介绍了一种基于策略模式的积分货币转换系统实现方案。该系统包括抽象策略接口定义、具体策略实现、策略容器和策略管理类等核心组件。通过策略模式实现了不同积分货币之间的灵活转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本博客是针对TapJoy的一个积分货币想转换的策略模式

(1)抽象策略类,拥有一个转换和一个执行方法

 

public interface ConvertStrategy {
    public BigDecimal convert(String loginId,Integer credit) throws ConvertException;

    public Boolean excute(String loginId,BigDecimal amount) throws ConvertHandlerException;
}

(2)具体策略了,实现抽象策略接口

 

 

@Component("defaultConvert")
public class DefaultConvert implements ConvertStrategy  {
    private static final Logger LOG = LoggerFactory.getLogger(DefaultConvert.class);
    @Resource(name = PfingoApiServiceName.TAPJOY_SERVICE)
    private TapJoyService TapJoyService;
    @Override
    public BigDecimal convert(String loginId, Integer credit,String plat,String env) throws ConvertException {
        LOG.info("enter convert");
        BigDecimal times = new BigDecimal("100");

        return (new BigDecimal(credit).multiply(times));
    }

    @Override
    public  Boolean excute(String loginId, BigDecimal amount,String plat,String env,String clientIp) throws ConvertHandlerException {
        LOG.debug("enter excute");
        LOG.debug("loginId:"+loginId);
        LOG.debug("amount:"+amount);
        LOG.debug("plat:"+plat);
        LOG.debug("env:"+env);
        LOG.debug("clientIp:"+clientIp);

        Map<String, Object> ht = new HashedMap();
        ht.put(Constant.CLIENT_IP, clientIp);
        ht.put(Constant.LOGIN_ID,loginId);
        ht.put("provider_id","SH_CC");
        ht.put("amount",amount);
        ht.put(Constant.CODE, null);
        ht.put(Constant.DESC,null);
        try {
            TapJoyService.tapTopUp(ht);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return ht.get(Constant.CODE).equals(StatusConstant.THOUSAND);
    }

    @PostConstruct
    public void register(){
        ConvertManager.set("default",this);
    }

}

(3)策略容器,持有策略对象

 

 

public class CreditConvertHandler  {
    private ConvertStrategy convertStrategy;
    public  CreditConvertHandler(String convert) throws ConvertNotFoundException {
        convertStrategy = ConvertManager.get(convert);
        if (convertStrategy == null){
            throw  new ConvertNotFoundException(convert+"not found");
        }

    }

    public BigDecimal convert(String loginId,Integer credit,String plat,String env) throws ConvertException{
        return convertStrategy.convert(loginId,credit,plat,env);
    }

    public Boolean excute(String loginId,BigDecimal amount,String plat,String env,String clientIp) throws ConvertHandlerException{
        return convertStrategy.excute(loginId,amount,plat,env,clientIp);
    }
}

(4)策略管理类,将所有策略对象以Map的形式存放。

 

 

public class ConvertManager {
    private static final Map<String,ConvertStrategy> map = new HashMap<>();
    public static ConvertStrategy get(String convertName){
        return map.get(convertName);
    }

    public static void set(String convertName,ConvertStrategy convertStrategy){
        map.put(convertName,convertStrategy);
    }
}


(5)使用策略

 

 

CreaditConvertHandler creditConvertHandler = new CreditConvertHandler("default");
BigDecimal amount = creditConvertHandler.convert(userId,currentcy,platform,version);
boolean isSuccess = creditConvertHandler.excute(userId,amount,platform,version,clientIp);

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值