【机房合作】策略+职责链在下机中的应用

时间:2022-10-02 21:55:03

一、刚开始使用职责链模式来实现下机时计算消费金额。


1、父类:CountCashManagerBLL


 abstract class CountCashManagerBLL
{
protected string name;
//管理者的上机
protected CountCashManagerBLL superior;
public CountCashManagerBLL(string name)
{
this.name = name;
}
//设置继任者,即所有计算金额的父类
public void SetSuperior(CountCashManagerBLL superior)
{
this.superior = superior;
}
//处理请求的抽象方法,即下机时的收费金额
public abstract OnOffLineEntity GetCash(OnOffLineEntity offline);

}
2、子类:FirstCountCashBLL

  //计算第一步时间间隔所花费的金额
public class FirstCountCashBLL : CountCashManagerBLL
{

public FirstCountCashBLL(string name)
: base(name)
{ }

public override OnOffLineEntity GetCash(OnOffLineEntity offline)
{
List<BasicDataEntity> List = new List<BasicDataEntity>();
List = FactoryRoom.CreateBasicData().InquireBasicData();
//如果上机时间 <=准备时间
if (int.Parse(offline.consumeTime)<=int.Parse(List[0].readyTime))
{
offline.consume = 0;
}
else
{
//传给下一级
superior.GetCash(offline);
}
return offline;
}
}

3、子类:SecondCountCashBLL

 //计算未达到单位递增时间的消费金额,大于准备时间
public class SecondCountCashBLL:CountCashManagerBLL
{

public override OnOffLineEntity GetCash(OnOffLineEntity offline)
{
CardEntity cardinfo=new CardEntity();
List<BasicDataEntity> List=FactoryRoom.CreateBasicData().InquireBasicData();
if (int.Parse(offline.consumeTime) < int.Parse(List[0].increaseTime))
{
if (cardinfo.cardType.Trim()=="VIP用户")
{
offline.consume = int.Parse(List[0].VIPRate); //VIP用户消费金额
}
else
{
offline.consume = int.Parse(List[0].generalRate); //一般用户消费金额
}
}
else
{
superior.GetCash(offline);
}
return offline;
}
}
4、子类:ThirdCountCashBLL

//计算已经达到单位上机时间的消费金额
public class ThirdCountCashBLL:CountCashManagerBLL
{
public override OnOffLineEntity GetCash(OnOffLineEntity offline)
{
List<BasicDataEntity> List = FactoryRoom.CreateBasicData().InquireBasicData();
double consumeTime= Convert.ToDouble(offline.consumeTime); //消费时间(将string类型转换为double类型)
double increaseTime = Convert.ToDouble(List[0].increaseTime);//单位递增时间
int VIPRate=int.Parse(List[0].VIPRate); //每单位时间VIP费用
int generalRate=int.Parse(List[0].generalRate); //每单位时间一般用户费用

double choseint = Math.Ceiling(consumeTime / increaseTime); //取整(向上取整。如54除以5 等于 6)


OnOffLineEntity cardinfo = new OnOffLineEntity();
if (cardinfo.cardType.Trim()=="VIP用户")
{
offline.consume = Convert.ToDecimal(VIPRate * choseint); //将double类型转换为decimal
}
else //如果是一般用户
{

offline.consume = Convert.ToDecimal(generalRate * choseint);
}

return offline;
}
}

感觉职责链虽然在一定程度上解耦了,但还是不够,于是加上了策略模式。职责链:负责算出有多少个单位时间。策略模式:对一般用户和VIP用户进行分情况讨论。代码如下。

职责链:

FirstCountCashBLl

    //计算第一步时间间隔所花费的时间
public class FirstCountCashBLL : CountCashManagerBLL
{

public override OnOffLineEntity GetCash(OnOffLineEntity offline)
{
List<BasicDataEntity> List = new List<BasicDataEntity>();
List = FactoryRoom.CreateBasicData().InquireBasicData();

double consumeTime = Convert.ToDouble(offline.consumeTime); //消费时间(将string类型转换为double类型

//如果上机时间 <=准备时间
if (consumeTime <= int.Parse(List[0].readyTime))
{
consumeTime = 0;
offline.consumeTime = consumeTime.ToString();
offline.choseint = 0;
}
else
{
//传给下一级
superior.GetCash(offline);
}
return offline;
}
}

SecondCountCashBLL:
 //计算未达到单位递增时间的消费金额,大于准备时间
public class SecondCountCashBLL:CountCashManagerBLL
{

public override OnOffLineEntity GetCash(OnOffLineEntity offline)
{
CardEntity cardinfo=new CardEntity();
List<BasicDataEntity> List=FactoryRoom.CreateBasicData().InquireBasicData();
double consumeTime = Convert.ToDouble(offline.consumeTime);
double increaseTime = Convert.ToDouble(List[0].increaseTime);

if (consumeTime < increaseTime)
{
consumeTime = increaseTime;
offline.consumeTime = consumeTime.ToString();
offline.choseint = 1;
}
else
{
superior.GetCash(offline);
}
return offline;
}
} //计算未达到单位递增时间的消费金额,大于准备时间
public class SecondCountCashBLL:CountCashManagerBLL
{

public override OnOffLineEntity GetCash(OnOffLineEntity offline)
{
CardEntity cardinfo=new CardEntity();
List<BasicDataEntity> List=FactoryRoom.CreateBasicData().InquireBasicData();
double consumeTime = Convert.ToDouble(offline.consumeTime);
double increaseTime = Convert.ToDouble(List[0].increaseTime);

if (consumeTime < increaseTime)
{
consumeTime = increaseTime;
offline.consumeTime = consumeTime.ToString();
offline.choseint = 1;
}
else
{
superior.GetCash(offline);
}
return offline;
}
}

ThirdCountCashBLL:

 public class ThirdCountCashBLL:CountCashManagerBLL
{
public override OnOffLineEntity GetCash(OnOffLineEntity offline)
{
List<BasicDataEntity> List = FactoryRoom.CreateBasicData().InquireBasicData();
double consumeTime= Convert.ToDouble(offline.consumeTime); //消费时间(将string类型转换为double类型)
double increaseTime = Convert.ToDouble(List[0].increaseTime);//单位递增时间
int VIPRate=int.Parse(List[0].VIPRate); //每单位时间VIP费用
int generalRate=int.Parse(List[0].generalRate); //每单位时间一般用户费用

double choseint = Math.Ceiling(consumeTime / increaseTime); //(有多少个单位消费时间)取整(向上取整。如54除以10 等于 6)
offline.choseint=choseint;
return offline;
}
}

策略模式:

CashSuperBLL:

public abstract class CashSuperBLL
{
public FirstCountCashBLL Bfirst = new FirstCountCashBLL();
public SecondCountCashBLL Bsecond = new SecondCountCashBLL();
public ThirdCountCashBLL Bthird = new ThirdCountCashBLL();
public abstract OnOffLineEntity acceptCash(OnOffLineEntity offline);

}
CashContextBLL:

   public class CashContextBLL
{
CashSuperBLL cs = null;
public CashContextBLL(OnOffLineEntity offline)
{
switch (offline.cardType)
{
case "一般用户":
cs = new GeneralCashBLL();
break;
case "VIP用户":
cs = new VIPCashBLL();
break;
}
}

//最后外观层只需要调用这个方法就行。

public OnOffLineEntity getConsumeCash(OnOffLineEntity offline)
{

return cs.acceptCash(offline);

}
}
GeneralCashBLL:

 public class GeneralCashBLL:CashSuperBLL
{
public override OnOffLineEntity acceptCash(OnOffLineEntity offline)
{

offline = Bfirst.GetCash(offline);
Bfirst.SetSuperior(Bsecond);
Bsecond.SetSuperior(Bthird);

List<BasicDataEntity> List = FactoryRoom.CreateBasicData().InquireBasicData();
int generalRate = int.Parse(List[0].generalRate); //每单位时间一般用户费用

offline.consume = Convert.ToDecimal(generalRate * offline.choseint);
return offline;
}
}

VIPCashBLL:

 public class VIPCashBLL:CashSuperBLL
{
public override OnOffLineEntity acceptCash(Entity.OnOffLineEntity offline)
{


List<BasicDataEntity> List = FactoryRoom.CreateBasicData().InquireBasicData();
int VIPRate = int.Parse(List[0].VIPRate); //每单位时间VIP费用
offline.consume = Convert.ToDecimal(VIPRate * offline.choseint);
return offline;
}
}