策略模式是一种定义一系列算法的方法, 从概念上看, 所有这些算法完成的都是相同的工作, 只是实现不同, 它可以以相同的方式调用所有的算法, 减少各种算法类与使用算法类之间的耦合.
与简单工厂不同的是, 策略模式将不同的子类算法(接口一样)抽象成一个类. 不同的初始化参数可以调用不同的算法. 而简单工厂是根据不同的初始化参数返回不同的子类对象.
收银机的主要代码 Form1.cs :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StrategyMethod
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
double total = 0.0d;
private void button_OK_Click(object sender, EventArgs e)
{
CashSuper csuper = CashFactory.createCashAccept(comboBox1.SelectedItem.ToString());
double totalPrices = 0d;
totalPrices = csuper.acceptCash(Convert.ToDouble(textBox_price.Text))
* Convert.ToDouble(textBox_num.Text);
total = total + totalPrices;
listBoxList.Items.Add("单价: " + textBox_price.Text + " 数量: " + textBox_num.Text + " "
+ comboBox1.SelectedItem + " 合计: " + totalPrices.ToString());
label_total.Text = total.ToString();
}
private void button_Reset_Click(object sender, EventArgs e)
{
CashContext cc = null;
switch (comboBox1.SelectedItem.ToString())
{
case "正常收费":
cc = new CashContext(new CashNormal());
break;
case "满300减100":
cc = new CashContext(new CashReturn("300", "100"));
break;
case "打8折":
cc = new CashContext(new CashRebate("0.8"));
break;
}
double totalPrices = 0d;
totalPrices = cc.GetResult(Convert.ToDouble(textBox_price.Text))
* Convert.ToDouble(textBox_num.Text);
total = total + totalPrices;
listBoxList.Items.Add("单价: " + textBox_price.Text + " 数量: " + textBox_num.Text + " "
+ comboBox1.SelectedItem + " 合计: " + totalPrices.ToString());
label_total.Text = total.ToString();
}
private void label1_Click(object sender, EventArgs e)
{
}
}
// 收款抽象基类
abstract class CashSuper
{
public abstract double acceptCash(double money);
}
// 正常收费子类
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 moneyRebate * money;
}
}
// 返利收费子类
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 - Math.Floor(money / moneyCondition) * moneyReturn;
}
return result;
}
}
// 现金收费工厂类
class CashFactory
{
public static CashSuper createCashAccept(string type)
{
CashSuper cs = null;
switch(type)
{
case "正常收费":
cs = new CashNormal();
break;
case "满300返100":
CashReturn cr1 = new CashReturn("300", "100");
cs = cr1;
break;
case "打8折":
CashRebate cr2 = new CashRebate("0.8");
cs = cr2;
break;
}
return cs;
}
}
// 策略模式
class CashContext
{
private CashSuper cs;
public CashContext(CashSuper csuper)
{
this.cs = csuper;
}
public double GetResult(double money)
{
return cs.acceptCash(money);
}
}
}