//後臺代碼
namespace Calculator
{
//商場管理策略模式(反射)
public partial class Shop : Form
{
public Shop()
{
InitializeComponent();
}
DataSet ds; //用於存放配置文件信息
double total = 0.0d; //用於總計
private void Shop_Load(object sender, EventArgs e)
{
//讀取配置文件
ds = new DataSet();
ds.ReadXml(Application.StartupPath+"\\CashAcceptType.xml");
//將讀取到的記錄綁定到下拉列表框中
foreach (DataRowView dr in ds.Tables[0].DefaultView)
{
cbxType.Items.Add(dr["name"].ToString());
}
cbxType.SelectedIndex = 0;
}
private void btn_Add_Click(object sender, EventArgs e)
{
CashContext cc = new CashContext();
//根據用戶選項,查詢用戶選擇項的相關行
DataRow dr = ((DataRow[])ds.Tables[0].Select("name='" + cbxType.SelectedItem.ToString() + "'"))[0];
//聲明一個對象參數數組
object[] args = null;
//若有參數則將參數分割成字符串數組,用於實例化時所用的參數
if (dr["para"].ToString() != "")
args = dr["para"].ToString().Split(',');
//通過反射實例化出相應的算法對象
cc.setBehavior((CashSuper)Assembly.Load("Calculator").CreateInstance("Calculator." + dr["class"].ToString(), false, BindingFlags.Default, null, args, null, null));
//cc.setBehavior(new CashNormal());
double totalPrices = 0d;
totalPrices = cc.GetResult(Convert.ToDouble(tb_Price.Text)*Convert.ToDouble(tb_Count.Text));
total = total + totalPrices;
lb_List.Items.Add("單價:"+tb_Price.Text+" 數量:"+tb_Count.Text+" "+cbxType.SelectedItem+" 合計"+totalPrices.ToString());
lb_total.Text = total.ToString();
}
}
}
//計算模式類
namespace Calculator
{
class CashContext
{
//聲明一個現金收費父類對象
private CashSuper cs;
public void setBehavior(CashSuper csuper)
{
this.cs = csuper;
}
public double GetResult(double money)
{
return cs.acceptCash(money);
}
}
//現金收取父類
abstract class CashSuper
{
//抽象方法:收取現金,參數爲原價,返回爲當前價
public abstract double acceptCash(double money);
}
//正常收費,繼承CashSuper
class CashNormal : CashSuper
{
public override double acceptCash(double money)
{
return money;
}
}
//打折收費,繼承CashSuper
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;
}
}
//返利收費,CashSuper
class CashReturn : CashSuper
{
private double moneyCondition = 0.0d;
private double moneyReturn = 0.0d;
//初始化時必須要輸入返利條件和返利值,比如慢300返100
//則moneyCondition爲300,moneyReturn爲100
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;
}
}
}
//XML數據
<?xml version="1.0" encoding="utf-8" ?>
<CashAcceptType>
<type>
<name>正常收費</name>
<class>CashNormal</class>
<para></para>
</type>
<type>
<name>滿300返100</name>
<class>CashReturn</class>
<para>300,100</para>
</type>
<type>
<name>滿200返50</name>
<class>CashReturn</class>
<para>200,50</para>
</type>
<type>
<name>打8折</name>
<class>CashRebate</class>
<para>0.8</para>
</type>
<type>
<name>打7折</name>
<class>CashRebate</class>
<para>0.7</para>
</type>
</CashAcceptType>