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();
        }
    }
    

      

  • 相关阅读:
    DNS服务器出错造成“不知道这样的主机”
    downadup.B蠕虫病毒处理手记
    今天新接触到一个名词——GSV
    客户端获取SQL服务端的MAC
    关于SQL事务的测试
    ftp://ftp.microsoft.com
    AJAX.DLL的使用
    "界面规则层与业务规则层"让我想开了
    客户端cookie也会传到服务端的Request.Params?
    Ext.Fx
  • 原文地址:https://www.cnblogs.com/song-wentao/p/8036700.html
Copyright © 2011-2022 走看看