策略模式:
它定义了算法家族,分别封装起来,让他们之间可以互相替换。
此模式让算法变化,不会影响到使用算法的客户。
结构图:
模式的组成:
环境类(Context):用一个ConcreteStrategy对象来配置。维护一个对Strategy对象的引用。
可定义一个接口来让Strategy访问它的数据。
抽象策略类(Strategy):定义所有支持的算法的公共接口。
Context使用这个接口来调用某ConcreteStrategy定义的算法。
具体策略类(ConcreteStrategy):以Strategy接口实现某具体算法。
抽象算法类:
// 抽象算法类
Abstract class Strategy
{
public abstract void AlgorithmInterface();
}
ConcreStrategy,封装了具体的算法或行为:
// 具体算法A
class ContreteStrategyA: Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("算法A实现");
}
}
// 具体算法B
class ContreteStrategyB: Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("算法B实现");
}
}
// 具体算法C
class ContreteStrategyC: Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("算法C实现");
}
}
Context,用一个ConcreteStrategy来配置,维护一个Strategy对象的引用。
// 上下文
class Context
{
Strategy strategy;
public Context()
{
this.strategy = strategy;
}
// 上下文接口
public void ContextInterface()
{
strategy.AlgorithmInterface();
}
}
客户端调用:
static void Main(string[] args)
{
Context context;
context = new Context(new ConcreteStrategyA());
context.ContextInterface();
context = new Context(new ConcreteStrategyB());
context.ContextInterface();
context = new Context(new ConcreteStrategyC());
context.ContextInterface();
Console.Read();
}
优点:
1. Strategy类可以为Context提供一系列可重用的算法或行为。
有助于抽象行为。每个行为独立,方便测试。
2.Strategy子类可以提供相同行为的不同实现。方便扩展。
缺点:
1. 客户端需要知道所有的策略类,以便决定使用哪种策略。将所有策略暴露出来。
(这种方式可以与简单工厂模式结合。进一步降低客户端耦合)
2. 造成很多Strategy子类。每扩展一次功能,需要增加子类。
举例:实现一个商场促销功能。
界面如下:
代码如下:
CashSuper:
abstract class CashSuper
{
public abstract double acceptCash(double money);
}
正常收费,打着,满减子类实现:
/// <summary>
/// 正常收费子类
/// </summary>
class CashNormal : CashSuper
{
public override double acceptCash(double money)
{
return money;
}
}
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;
}
}
class CashReturn : CashSuper
{
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 - money / moneyCondition * moneyReturn;
return result;
}
}
CashContext:
class CashContext
{
private CashSuper cs;
public CashContext(string type)
{
switch (type)
{
case "正常收费":
cs = new CashNormal();
break;
case "满300返100":
cs = new CashReturn("300","100");
break;
case "打八折":
cs = new CashRebate("0.8");
break;
case "打五折":
cs = new CashRebate("0.5");
break;
}
}
public double GetResult(double money)
{
return cs.acceptCash(money);
}
}
客户端调用:
public partial class Form1 : Form
{
double total = 0.0d;
public Form1()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
double total = 0.0d;
CashContext csuper = new CashContext(cbxType.SelectedItem.ToString());
double totalPrices = 0d;
totalPrices = csuper.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));
total = total + totalPrices;
lblList.Items.Add("单价:" + txtPrice.Text + "数量:" + txtNum.Text + " " + cbxType.SelectedItem + "合计:" + totalPrices.ToString());
lbResult.Text = total.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
cbxType.Items.AddRange(new object[] { "正常收费", "打八折", "满300返100", "打五折" });
cbxType.SelectedIndex = 0;
}
}
项目代码路径: