栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

《大话设计模式》——策略模式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

《大话设计模式》——策略模式

1.简单工厂实现 1.1.现金收费抽象类

抽象基类提供函数接口,不需要提供函数实现,此处区别于虚函数。

abstract class CashSuper{
	public abstract double acceptCash(double money);
}
1.2.正常收费子类
class CashNormal:CashSuper{
	public double acceptCash(double money){
		return money;
	}
}
1.3.打折收费子类
class CashRebate:CashSuper{
	private double moneyRebate=1d;
	
	public CashRebate(string moneyRebate){
		this.moneyRebate=double.Parse(moneyRebate);
	}
	public override double acceptCash(double money){
		return money*moneyRebate;
	}
}
1.4.返利收费子类
class CashReturn{
	private double moneyCondition=0.0d;
	private double moneyReturn=0.0d;
	public CashReturn(string moneyCondition,string moneyReturn){
		this.moneyCondition=double.Parse(moneyCondition);
		this.moneyReturn=double.Parse(moneyReturn);
	}
	public override double acceptCash(double money){
		double result=money;
		if(money>=moneyCondition)
			result=money-Math.Floor(money/moneyCondtion)*moneyReturn;
		
		return result;
	}
}
1.5.现金收费工厂
class CashFactory{
	public static CashSuper createCashAccept(string type){
		CashSuper cs=null;
		switch(type){
			case "正常收费":
				cs=new CashNormal;
				break;
			case "满300返100":
				cs=new CashReturn("300","100");
				break;
			case "打8折":
				cs=new CashRebate("0.8");
				break;
		}
		return cs
	}
}
1.6.客户端实现
double total=0.0d;
private void btnOk_Click(object sender,EventArgs e){
	CashSuper csuper=CashFactory.createCashAccept(cbxType.SelectedItem.ToString());
	double totalPrices=0d;
	totalPrices=csuper.acceptCash(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(txtNum.Text));
	total=total+totalPrices;
	lbxList.Item.Add("单价:"+textPrice.Text+"数量:"+txtNum.Text+" "+cbxType.SelectedItem+"合计:"+totalPrices.ToString());
	lblResult.Text=total.ToString();
}
2.策略模式实现

之前的CashSuper、CashNormal、CashRebate和CashReturn都不用更改,只需添加一个CashContext类,并修改客户端就好了。

2.1.CashContext类
class CashContext{
	private CashSuper cs;

	public CashContext(CashSuper csuper){
		this.cs=csuper;
	}

	public double GetResult(double money){
		return cs.acceptCash(money);
	}
}
2.2.客户端主要代码
double total =0.0d;
private void btnOk_Click(object sender,EventArgs e){
	CashContext cc=null;
	switch(cbxType.SelectedItem.ToString()){
		case "正常收费":
			cc=new CashContext(new CashNormal());
			break;
		case "满300返100":
			cc=new CashContext(new CashReturn("300","100"));
			break;
		case "打8折":
			cc=new CashContex(new CashRebate("0.8");
			break;
	}
	double totalPrices=0d;
	totalPrices==cc.GetResult(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(textNum.Text));
	total=total+totalPrices;
	lbxList.Items.Add("单价:"+txtPrice.Text+"数量:"+txtNum.Text+" "+cbxType.SelectedItem+"合计:"+totalPrices.ToString());
	lblResult.Text=total.ToString();
}
3.策略与简单工厂结合 3.1改造后的CashContext
class CashContext{
	CashSuper cs=null;

	public CashContext(string type){
		switch(type){
			case "正常收费":
				cs=new CashNormal();
				break;
			case "满300返100":
				cs=new CashReturn("300","100");
				break;
			case "打8折":
				cs=new CashRebate("0.8");
				break;				
		}
	}

	public double GetResult(double money){
		return cs.acceptCash(money);
	}
}
3.2客户端代码
double total=0.0d;
private void btnOk_Click(object sender,EventArgs e){
	CashContext csuper=new CashContext(cbxType.SelectedItem.ToString());
	double totalPrices=0d;
	totalPrices=csuper.GetResult(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(txtNum.Text));
	total=total+totalPrices;
	lbxList.Items.Add("单价:"+txtPrice.Text+"数量:"+txtNum.Text+" "+cbxType.SelectedItem+"合计:"+totalPrices.ToString());
	lblResult.Text=total.ToString();
}

简单工厂模式的做法,客户端需要认识两个类,CashSuper和CashFactory,而策略模式与简单工厂结合的用法,客户端只需要认识CashContext就可以了,如此耦合进一步降低。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/295012.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号