C# 策略模式
/// <summary>
/// 1现金收费抽象类
/// </summary>
abstract class CashSuper
{
public abstract double acceptCash(double money);
}
/// <summary>
/// 2返利收费子类
/// </summary>
class CashReturn : CashSuper
{
private double moneyCondition = 0.0d;
private double moneyReturn = 0.0d;
public CashReturn(string moneyCondtion,string moneyReturn)
{
this.moneyCondition = double.Parse(moneyCondtion);
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;
}
}
/// <summary>
/// 3打折收费子类
/// </summary>
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;
}
}
/// <summary>
/// 4现金收费模式
/// </summary>
class CashNormal : CashSuper
{
public override double acceptCash(double money)
{
return money;
}
}
//5CashFactory类
class CashFactory
{
public static CashSuper createCashAccept(string type)
{
CashSuper cs = null;
switch (type)
{
case "正常收费":
cs =new CashNormal();
break;
case "满300返100":
CashReturn crl = new CashReturn("300","100");
cs = crl;
break;
case "打8折":
CashRebate cr2 = new CashRebate("0.8");
cs = cr2;
break;
default:
break;
}
return cs;
}
}
//6CashContext
class CashContext
{
#region v2.0
//private CashSuper cs;
//public CashContext(CashSuper csuper)
//{
// this.cs = csuper;
//}
//public double GetResult(double money)
//{
// return cs.acceptCash(money);
//}
#endregion
#region v3.0
CashSuper cs = null;
public CashContext()
{
}
public CashContext(string type)
{
switch (type)
{
case "正常收费":
CashNormal cs0 = new CashNormal();
cs = cs0;
break;
case "满300返100":
CashReturn cs1 =new CashReturn("300","100");
cs = cs1;
break;
case "打8折":
CashRebate cs2 = new CashRebate("0.8");
cs = cs2;
break;
}
}
public double GetResult(double money)
{
return cs.acceptCash(money);
}
#endregion
}
class Program
{
static void Main(string[] args)
{
#region v0.1
//double total = 0.0d;
//Console.WriteLine("类型(正常收费,满300返100,打8折)");
//string type = Console.ReadLine();
//Console.ReadKey();
//CashSuper csuper = CashFactory.createCashAccept(type);
//double totalPrice = 0d;
//Console.WriteLine("价格:");
//string price = Console.ReadLine();
//Console.ReadKey();
//Console.WriteLine("数量:");
//string num = Console.ReadLine();
//Console.ReadKey();
//totalPrice = csuper.acceptCash(Convert.ToDouble(price))* Convert.ToDouble(num);
//total = total + totalPrice;
//Console.WriteLine("合计:"+ totalPrice);
#endregion
#region v0.2
//double total = 0.0d;//用于总计
//CashContext cc = null;
//Console.WriteLine("类型(正常收费,满300返100,打8折)");
//string type = Console.ReadLine();
//Console.ReadKey();
//switch (type)
//{
// 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;
//Console.WriteLine("价格:");
//string price = Console.ReadLine();
//Console.ReadKey();
//Console.WriteLine("数量:");
//string num = Console.ReadLine();
//Console.ReadKey();
//totalPrices = cc.GetResult(Convert.ToDouble(price)*Convert.ToDouble(num));
//total = total + totalPrices;
//Console.WriteLine("合计:" + totalPrices);
#endregion
#region 0.3
double total = 0.0d;
Console.WriteLine("类型(正常收费,满300返100,打8折)");
string type = Console.ReadLine();
Console.ReadKey();
Console.WriteLine("价格:");
string price = Console.ReadLine();
Console.ReadKey();
Console.WriteLine("数量:");
string num = Console.ReadLine();
Console.ReadKey();
CashContext csuper = new CashContext(type);
double totalPrices = 0d;
totalPrices = csuper.GetResult(Convert.ToDouble(price)*Convert.ToDouble(num));
Console.WriteLine("合计:" + totalPrices);
Console.WriteLine(total);
#endregion
}
}