策略模式
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();
}
——————————————————————————————————————————————
一、概念
策略模式(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();
}
相关文章推荐
- 设计模式之Strategy(策略)
- 设计模式(20)-策略模式(Strategy)
- 策略模式之C++版本
- 应用泛型的策略模式
- 对象模型--策略、模式、应用 笔记1
- [浪子学编程][MS Enterprise Library]ObjectBuilder之设计模式(一):策略模式
- (22)策略模式-Strategy Pattern-康熙收复台湾-明珠和索额图的不同策略
- 从诸葛亮的三个锦囊妙计谈策略模式
- 《Java与模式》学习笔记之九-----策略模式(Strategy Pattern)
- AspectJ实现设计模式(二)——策略模式
- 设计模式之Strategy(策略)
- 设计模式之Strategy(策略)
- 策略模式之C#版本(原创翻译版本)
- 设计模式实战(一)——使用策略模式(strategy pattern)实现多关键字排序
- Delphi模式编程之策略模式(下)
- 設計模式之Strategy(策略)
- 深入浅出策略模式
- 设计模式——策略模式
- 设计模式之策略模式(Strategy)
- 对象模型--策略、模式、应用 笔记2---选择对象