【设计模式】【策略模式 打折算法】

本文介绍了Java联盟网站上关于折扣策略的实现,包括VIP折扣和旧书折扣算法,通过DiscountContext类组合不同策略,实现了灵活的定价逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public interface DiscountStrategy
{
	//定义一个用于计算打折价的方法
	double getDiscount(double originPrice); 
}


/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
//实现DiscountStrategy接口,实现对VIP打折的算法
public class VipDiscount 
	implements DiscountStrategy
{
	//重写getDiscount()方法,提供VIP打折算法
	public double getDiscount(double originPrice)
	{
		System.out.println("使用VIP折扣...");
		return originPrice * 0.5;
	}
}


/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class OldDiscount
	implements DiscountStrategy
{
	//重写getDiscount()方法,提供旧书打折算法
	public double getDiscount(double originPrice)
	{
		System.out.println("使用旧书折扣...");
		return originPrice * 0.7;
	}
}



/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class DiscountContext
{
	//组合一个DiscountStrategy对象
	private DiscountStrategy strategy;
	//构造器,传入一个DiscountStrategy对象
	public DiscountContext(DiscountStrategy strategy)
	{
		this.strategy  = strategy;
	}
	//根据实际所使用的DiscountStrategy对象得到折扣价
	public double getDiscountPrice(double price) 
	{
		//如果strategy为null,系统自动选择OldDiscount类
		if (strategy == null)
		{
			strategy = new OldDiscount();
		}
		return this.strategy.getDiscount(price);
	}
	//提供切换算法的方法
	public void changeDiscount(DiscountStrategy strategy)
	{
		this.strategy = strategy;
	}
}
 



/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class StrategyTest
{
	public static void main(String[] args) 
	{
		//客户端没有选择打折策略类
		DiscountContext dc = new DiscountContext(null);
		double price1 = 79;
		//使用默认的打折策略
		System.out.println("79元的书默认打折后的价格是:" 
			+ dc.getDiscountPrice(price1));
		//客户端选择合适的VIP打折策略
		dc.changeDiscount(new VipDiscount());
		double price2 = 89;
		//使用VIP打折得到打折价格
		System.out.println("89元的书对VIP用户的价格是:" 
			+ dc.getDiscountPrice(price2));
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值