zoukankan      html  css  js  c++  java
  • spring boot 策略模式实践

    package com.swt.demo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.math.BigDecimal;
    
    @RestController
    @SpringBootApplication
    public class DemoApplication {
    
    	@Autowired
    	private StrategyContext strategyContext;
    
    	@RequestMapping("calculatePrice")
    	public BigDecimal calculatePrice(String memberLevel) {
    		return strategyContext.calculatePrice(memberLevel);
    	}
    
    	public static void main(String[] args) {
    		SpringApplication.run(DemoApplication.class, args);
    	}
    }
    

      

    package com.swt.demo;
    
    import java.math.BigDecimal;
    
    public interface Strategy {
    
        /**
         * 计算价格
         * @return
         */
        BigDecimal calculatePrice();
    }
    

      

    package com.swt.demo;
    
    import org.springframework.stereotype.Component;
    
    import java.math.BigDecimal;
    
    @Component("generalMember")
    public class GeneralMember implements Strategy {
        @Override
        public BigDecimal calculatePrice() {
            // 普通会员没有折扣,直接返回原价
            return new BigDecimal("100");
        }
    }
    

      

    package com.swt.demo;
    
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Service;
    
    import java.math.BigDecimal;
    
    @Component("vipMember")
    public class VipMember implements Strategy {
        @Override
        public BigDecimal calculatePrice() {
            // VIP会员打8折
            return new BigDecimal("80");
        }
    }
    

      

    package com.swt.demo;
    
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Service;
    
    import java.math.BigDecimal;
    
    @Component("superMember")
    public class SuperMember implements Strategy {
        @Override
        public BigDecimal calculatePrice() {
            // 超级会员打1折
            return new BigDecimal("10");
        }
    }
    

      

    package com.swt.demo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.math.BigDecimal;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    @Service
    public class StrategyContext {
        private final Map<String, Strategy> strategyMap = new ConcurrentHashMap<>();
    
        /**
         * 注入所以实现了Strategy接口的Bean
         * @param strategyList
         */
        /*@Autowired
        public StrategyContext(List<Strategy> strategyList) {
            strategyMap.clear();
            Integer index = 0;
            for (Strategy strategy : strategyList) {
                strategyMap.put(index.toString(), strategy);
                index++;
            }
        }*/
    
        /**
         * 注入所以实现了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) {
            return strategyMap.get(memberLevel).calculatePrice();
        }
    }
    

      

  • 相关阅读:
    Counting Stars hdu
    Color it hdu
    steins;Gate
    原根
    3-idiots
    Tree
    洛谷P1352 没有上司的舞会
    洛谷P1131 时态同步
    洛谷P3177 树上染色
    Codeforces Round #617 (Div. 3)
  • 原文地址:https://www.cnblogs.com/song-wentao/p/8036700.html
Copyright © 2011-2022 走看看