class CashContext
{
private CashSuper cs;
public CashContext(CashSuper csuper)
{
this.cs = csuper;
}
public double GetResult(double money)
{
return cs.acceptCash(money);
}
} 将客户端程序改为://客户端主要代码
double total = 0.0d;
private void btnOK_Click(object sender, EventArgs e)
{
CashContext cc = null;
switch (cbxType.SelectedItem.ToString())//根据下拉选择框,将相应的策略对象作为参数传入CashContext的对象中
{
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(txtPrice.Text) * Convert.ToDouble(txtNum.Text));//通过Context的GetResult方法的调用,可以得到收取费用的结果,让具体算法与客户进行了隔离
total = total + totalPrices;
lbxList.Items.Add("单价:" + txtPrice.Text + "数量:" + txtNum.Text + "" + cbxType.SelectedItem + "合计:" + totalPrices.ToString());
lblResult.Text = total.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
cbxType.Items.Add("正常收费");
cbxType.Items.Add("满300返100");
cbxType.Items.Add("打8折");
}
结合上面的代码和下面的释义不难看出,其实两个的差别很微妙,Factory是直接创建具体的对象并用该对象去执行相应的动作,而Context将这个操作给了Context类,没有创建具体的对象,实现的代码的进一步封装,客户端代码并不需要知道具体的实现过程。
class CashContext
{
CashSuper cs = null; //声明一个CashSuper对象
public CashContext(string type)//注意参数不是具体的收费策略对象,而是一个字符串,表示收费类型
{
switch (type)
{
//将实例化具体策略的股从横由客户端转移到Context类中,简单工厂的应用
case "正常收费":
CashNormal cs0 = new CashNormal();
cs = cs0;
break;
case "满300返100":
CashReturn cr1 = new CashReturn("300", "100");
cs = cr1;
break;
case "打8折":
CashRebate cr2 = new CashRebate("0.8");
cs = cr2;
break;
}
}
public double GetResult(double money)
{
return cs.acceptCash(money);
}
}客户端的程序为:
//客户端主要代码
double total = 0.0d;
private void btnOK_Click(object sender, EventArgs
e)
{
CashContext csuper = new CashContext
(cbxType.SelectedItem.ToString());//根据下拉选择框,将相应
的算法类型字符串传入CashContext的对象中
double totalPrices = 0d;
totalPrices = csuper.GetResult
(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble
(txtNum.Text));
total = total + totalPrices;
lbxList.Items.Add("单价:" + txtPrice.Text + "
数量:" + txtNum.Text + "" + cbxType.SelectedItem + "合计
:" + totalPrices.ToString());
lblResult.Text = total.ToString();
}
private void Form1_Load(object sender, EventArgs
e)
{
cbxType.Items.Add("正常收费");
cbxType.Items.Add("满300返100");
cbxType.Items.Add("打8折");
} 简单工厂模式我需要让客户端认识两个类CashSuper、CashFactory而策略模式与简单工厂结合的用法,客户端只需要认识一个类CashContext就可以了,耦合更为降低原文:http://blog.csdn.net/lxy344x/article/details/23032699