设计模式-7-策略模式+工厂模式

简介:策略模式+工厂模式

目的:策略模式下的算法选择工厂化

总结:策略模式+工厂模式

组成:对象行为, 算法接口, 算法接口实现, 算法工厂

1,对象行为

package com.design.g.strategy.factory;
/**
 * 顾客
 */
public class Customer {
    
    //消费金额
    private Double amount = 0D;     
    
    //价格计算策略   初始化为不打折计算策略
    private CalculatePriceService calculatePriceService = new CommonCalculatePriceServiceImpl();
    
    /**
     * 根据顾客的购买金额返回相应的价格计算策略
     * @param amount
     */
    public void buy(Double amount){
        
        this.amount = amount;
        
//        if(amount > 3000){      //金牌会员计算策略
//            calculatePriceService = new GoldCalculatePriceServiceImpl();
//        }else if(amount >2000){ //银牌会员计算策略
//            calculatePriceService = new SilverCalculatePriceServiceImpl();
//        }else if(amount > 1000){//VIP用户计算策略
//            calculatePriceService = new VipCalculatePriceServiceImpl();
//        }else{                  //普通会员计算策略  
//            calculatePriceService = new CommonCalculatePriceServiceImpl();
//        }
        
        /**
         * 使用工厂模式获取价格计算策略
         */
        calculatePriceService = PriceFactory.getPriceServiceImpl(this);
    }
    
    /**
     * 获取计算后的价格
     */
    public Double calculateLastAmount(){
        return calculatePriceService.calculatePrice(amount);
    }

    
    public Double getAmount() {
        return amount;
    }
}

2,算法接口

package com.design.g.strategy.factory;
/**
 * 策略接口
 */
public interface CalculatePriceService {

    /**
     * 根据总价计算结算价格
     * @param originPrice
     * @return
     */
    Double calculatePrice(Double originPrice);
}

3,算法接口实现

package com.design.g.strategy.factory;
/**
 * 策略实现 - 普通会员价格计算   不打折
 */
public class CommonCalculatePriceServiceImpl implements CalculatePriceService {

    @Override
    public Double calculatePrice(Double originPrice) {
        return originPrice;
    }

}
package com.design.g.strategy.factory;
/**
 * 策略实现 - VIP用户价格计算  打8折
 */
public class VipCalculatePriceServiceImpl implements CalculatePriceService {

    @Override
    public Double calculatePrice(Double originPrice) {
        return originPrice * 0.8;
    }

}
package com.design.g.strategy.factory;
/**
 * 策略实现 - 银牌会员价格计算   打6折
 */
public class SilverCalculatePriceServiceImpl implements CalculatePriceService {

    @Override
    public Double calculatePrice(Double originPrice) {
        return originPrice * 0.6;
    }

}
package com.design.g.strategy.factory;
/**
 * 策略实现 - 金牌会员价格计算   打5折
 */
public class GoldCalculatePriceServiceImpl implements CalculatePriceService {

    @Override
    public Double calculatePrice(Double originPrice) {
        return originPrice * 0.5;
    }

}

4,算法工厂

package com.design.g.strategy.factory;
/**
 * 价格计算策略工厂
 */
public class PriceFactory {
    
    //私有化构造函数
    private PriceFactory(){};
    
    
    //根据消费金额获取对应的计算策略
    public static CalculatePriceService getPriceServiceImpl(Customer customer){
        
        if(customer.getAmount() > 3000){      //金牌会员计算策略
            return new GoldCalculatePriceServiceImpl();
        }else if(customer.getAmount() >2000){ //银牌会员计算策略
            return new SilverCalculatePriceServiceImpl();
        }else if(customer.getAmount() > 1000){//VIP用户计算策略
            return new VipCalculatePriceServiceImpl();
        }else{                  //普通会员计算策略  
            return new CommonCalculatePriceServiceImpl();
        }
    }

}

5,Main

package com.design.g.strategy.factory;
/**
 * 策略模式  (含工厂模式)
 */
public class MainTest {

    public static void main(String[] args) {
        Customer customer = new Customer();
        Double amount = 0D;
        Double lastAmount = 0D;
        
        amount = 5000D;
        customer.buy(amount);
        lastAmount = customer.calculateLastAmount();
        getLastAmount(lastAmount, amount);
        
        amount = 2500D;
        customer.buy(amount);
        lastAmount = customer.calculateLastAmount();
        getLastAmount(lastAmount, amount);
        
        amount = 1500D;
        customer.buy(amount);
        lastAmount = customer.calculateLastAmount();
        getLastAmount(lastAmount, amount);
        
        amount = 100D;
        customer.buy(amount);
        lastAmount = customer.calculateLastAmount();
        getLastAmount(lastAmount, amount);
    }
    
    private static void getLastAmount(Double lastAmount, Double amount){
        System.out.println("最终价格 : " + lastAmount +" ;总共打折" + (lastAmount/amount));
    }
}

6,Result

最终价格 : 2500.0 ;总共打折0.5
最终价格 : 1500.0 ;总共打折0.6
最终价格 : 1200.0 ;总共打折0.8
最终价格 : 100.0 ;总共打折1.0
原文地址:https://www.cnblogs.com/wanhua-wu/p/7205567.html