策略模式
2010-05-20 14:53
190 查看
[b]一、概念[/b]
策略模式(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();
}
Over。O(∩_∩)O
/article/5403550.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();
}
Over。O(∩_∩)O
/article/5403550.html
相关文章推荐
- 设计模式之策略模式
- 策略模式
- 设计模式一:策略模式
- Java设计模式学习之一---策略模式
- [设计模式]策略模式
- 设计模式:策略模式(Strategy)
- 我读设计模式之策略模式(Strategy pattern)
- 设计模式C++实现(2)——策略模式
- android架构之模板方法模式和策略模式
- 大话设计模式之策略模式
- Java设计模式-策略模式
- 23种设计模式之策略模式
- C++设计模式之二十一:Strategy(策略模式)
- 设计模式和原则:策略模式
- 设计模式之策略模式(Strategy)
- 策略模式
- First Head 设计模式之策略模式(Strategy Pattern)
- 设计模式-策略模式(Java)
- 设计模式之策略模式
- Java中策略设计模式