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

  

 

posted on 2017-12-14 11:14  小小程序员的梦想  阅读(7106)  评论(0编辑  收藏  举报

导航