您的位置:首页 > 其它

策略模式

2014-09-05 19:47 176 查看
转自:http://www.cnblogs.com/colinsong/archive/2009/03/02/1401723.html

——————————————————————————————————————————————

一、概念

策略模式(Strategy):它定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法的变化不会影响到使用算法的客户。(原文:The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.)

[align=center]namespace ColinSong.DesignPattern.Strategy
2[/align]

好,算法完成了,下面介绍客户端,界面如图2所示:

Code
private void btnOK_Click(object sender, EventArgs e)
{
\\只需要知道这个Context类,实例化它;
Billing billing = new Billing(cbxBillingType.Text);
\\并调用它提供的方法,即可完成我们需要的功能。
double charge = billing.GetResult(double.Parse(txtPrice.Text),
int.Parse(txtQuantity.Text));
totalCash = totalCash + charge;
string itemShow = "单价:"+txtPrice.Text
+ "\t数量:"+txtQuantity.Text +
".\t实收:"+ charge.ToString()
+ "\t"+cbxBillingType.Text;
list.Items.Add(itemShow);
lblSum.Text = totalCash.ToString();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: