【含全部源码】Spring 实现策略模式

策略接口

package org.apel.bowen.controller;
 
import java.math.BigDecimal;
/**
 * 计算价格的接口
 * @author Bowin
 *
 */
public interface Strategy {
 /**
     * 计算价格
     * @return
     */
   public  BigDecimal calculatePrice();
}

2.实现接口

package org.apel.bowen.controller;
 
import java.math.BigDecimal;
 
import org.springframework.stereotype.Service;
/**
 * 普通用户
 * @author Bowin
 *
 */
@Service("generalMember")
public class GeneralMember implements Strategy{
 
 @Override
 public BigDecimal calculatePrice() {
  return new BigDecimal("100");
 }
 
}
package org.apel.bowen.controller;
 
import java.math.BigDecimal;
 
import org.springframework.stereotype.Service;
/**
 * vip用户
 * @author Bowin
 *
 */
@Service("vipMember")
public class VipMember  implements Strategy{
 
 @Override
 public BigDecimal calculatePrice() {
  return new BigDecimal("80");
 }
 
}
package org.apel.bowen.controller;
 
import java.math.BigDecimal;
 
import org.springframework.stereotype.Service;
/**
 * 超级会员
 * @author Bowin
 *
 */
@Service("superMember")
public class SuperMember implements Strategy{
 
 @Override
 public BigDecimal calculatePrice() {
  return new BigDecimal("1");
 }
 
}

3.策略控制器


package org.apel.bowen.controller;
 
import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/**
 * 策略管理器
 * @author Bowin
 *
 */
@Component
public class StrategyContext {
 /**
  *使用线程安全的ConcurrentHashMap存储所有实现Strategy接口的Bean
  *key:beanName
  *value:实现Strategy接口Bean
  */
 private final Map<String, Strategy> strategyMap = new ConcurrentHashMap<>();
 
  /**
     * 注入所有实现了Strategy接口的Bean
     * @param strategyMap
     */
    @Autowired
    public StrategyContext(Map<String, Strategy> strategyMap) {
        this.strategyMap.clear();
        strategyMap.forEach((k, v)-> this.strategyMap.put(k, v));
    }
 
    /**
     * 计算价格
     * @param memberLevel   会员等级
     * @return              价格
     */
    public BigDecimal calculatePrice(String memberLevel) {
     if(!StringUtils.isEmpty(memberLevel)){
      return strategyMap.get(memberLevel).calculatePrice();
     }
  return null;
    }
}

4.客户端调用

package org.apel.bowen.controller;
 
import java.math.BigDecimal;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * 前端控制器
 * @author Bowin
 *
 */
@Controller
@RequestMapping
public class HelloController {
 
 
  @Autowired
   private StrategyContext strategyContext;
  
   @RequestMapping("calculatePrice")
   public @ResponseBody BigDecimal calculatePrice(String memberLevel) {
         return strategyContext.calculatePrice(memberLevel);
   }
  
}

5.访问测试

http://localhost:9999/calculatePrice?memberLevel=vipMember 返回 80


作者:咩咩文

来源链接:

https://blog.csdn.net/u011659172/article/details/79140561

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值