简单工厂实现商场收银系统:
策略模式实现商场收银系统:
定义了算法家族,分别封装起来,让他们可以互相转换,此模式让算法的变化,不会影响到使用算法的客户。
简单工厂:需要让客户端认识2个类,CashSuper和CashFactory
策略模式:需要让客户端认识4个类,CashContext和CashNormal,CashReturn,CashRebate
简单工厂和策略模式:需要让客户端认识1个类,CashContext
using System;
using System.Collections.Generic;
using System.Text;
namespace 商场管理软件
{
//现金收取工厂
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;
}
}
//现金收取父类
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;
//初始化时,必需要输入折扣率,如八折,就是0.8
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;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//客户端窗体程序(主要部分)
double total = 0.0d;
private void btnOk_Click(object sender, EventArgs e)
{
//利用简单工厂模式根据下拉选择框,生成相应的对象
CashSuper csuper = CashFactory.createCashAccept(cbxType.SelectedItem.ToString());
double totalPrices = 0d;
//通过多态,可以得到收取费用的结果
totalPrices = csuper.acceptCash(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 btnClear_Click(object sender, EventArgs e)
{
total = 0d;
txtPrice.Text = "0.00";
txtNum.Text = "0";
lbxList.Items.Clear();
lblResult.Text = "0.00";
}
}
}
策略模式实现商场收银系统:
定义了算法家族,分别封装起来,让他们可以互相转换,此模式让算法的变化,不会影响到使用算法的客户。
using System;
using System.Collections.Generic;
using System.Text;
namespace 商场管理软件
{
//收费策略Context
class CashContext
{
//声明一个现金收费父类对象
private CashSuper cs;
//设置策略行为,参数为具体的现金收费子类(正常,打折或返利)
public CashContext(CashSuper csuper)
{
this.cs = csuper;
}
//得到现金促销计算结果(利用了多态机制,不同的策略行为导致不同的结果)
public double GetResult(double money)
{
return cs.acceptCash(money);
}
}
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 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- Math.Floor(money / moneyCondition) * moneyReturn;
return result;
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double total = 0.0d;//用于总计
private void btnOk_Click(object sender, EventArgs e)
{
CashContext cc = null;
switch (cbxType.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(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 btnClear_Click(object sender, EventArgs e)
{
total = 0d;
txtPrice.Text = "0.00";
txtNum.Text = "1";
lbxList.Items.Clear();
lblResult.Text = "0.00";
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
策略模式和简单工厂结合实现商场收银系统:
即把判断代码放在CashContext类里面,而不是客户端里。
using System;
using System.Collections.Generic;
using System.Text;
namespace 商场管理软件
{
//现金收取工厂
class CashContext
{
CashSuper cs = null;
//根据条件返回相应的对象
public CashContext(string type)
{
switch (type)
{
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);
}
}
//现金收取父类
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;
//初始化时,必需要输入折扣率,如八折,就是0.8
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;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//客户端窗体程序(主要部分)
double total = 0.0d;
private void btnOk_Click(object sender, EventArgs e)
{
//利用简单工厂模式根据下拉选择框,生成相应的对象
CashContext csuper = new CashContext(cbxType.SelectedItem.ToString());
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 btnClear_Click(object sender, EventArgs e)
{
total = 0d;
txtPrice.Text = "0.00";
txtNum.Text = "0";
lbxList.Items.Clear();
lblResult.Text = "0.00";
}
}
}
简单工厂:需要让客户端认识2个类,CashSuper和CashFactory
策略模式:需要让客户端认识4个类,CashContext和CashNormal,CashReturn,CashRebate
简单工厂和策略模式:需要让客户端认识1个类,CashContext