职责链模式—上机
【摘要】
职责链模式的入口
/// <summary>
/// 该类是上机职责链的入口,用于b层调用
/// </summary>
public class ...
职责链模式的入口
/// <summary>
/// 该类是上机职责链的入口,用于b层调用
/// </summary>
public class OnLineChain
{
OnLineIsCardExit onLineIsCardExit = new OnLineIsCardExit();
OnLineLimitCash onLineLimitCash = new OnLineLimitCash();
OnLineIsOnLine onLineIsOnLine = new OnLineIsOnLine();
OnLineCardOnLine onLineCardOnLine = new OnLineCardOnLine();
public Hashtable doHandler(string cardID)
{
onLineIsCardExit.SetSuccessor(onLineLimitCash);
onLineLimitCash.SetSuccessor(onLineIsOnLine);
onLineIsOnLine.SetSuccessor(onLineCardOnLine);
return onLineIsCardExit.HandleRequest(cardID);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
用于上机的抽象类
/// <summary>
/// 该类用于上机的抽象类,定义了骨架
/// </summary>
public abstract class OnLineHandler
{
protected OnLineHandler successor;
protected CustomerFactory customerFactory = new CustomerFactory();
protected BasicdataFactory basicdataFactory = new BasicdataFactory();
protected CustomerEntity customerEntity = new CustomerEntity();
protected LineFactory lineFactory = new LineFactory();
protected LineEntity lineEntity = new LineEntity();
public void SetSuccessor(OnLineHandler successor)
{
this.successor = successor;
}
public abstract Hashtable HandleRequest(string cardID);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
用于正常上机的情况
/// <summary>
/// 该类用于最终正常上机情况的处理
/// </summary>
public class OnLineCardOnLine:OnLineHandler
{
public override Hashtable HandleRequest(string cardID)
{
Hashtable hashtable = new Hashtable();
CustomerIDAL customerIdal = customerFactory.Customer();
List<CustomerEntity> listCustomer = customerIdal.getCustomerByCardID(cardID);
//查询卡的类型 余额 姓名
string cType = listCustomer[0].cType;
decimal cCash = Convert.ToDecimal(listCustomer[0].cCash);
string cName = listCustomer[0].cName;
//查询卡的单位消费金额
RateManager rm = new RateManager();
decimal rate = rm.getRate(cType);
//当前服务器时间
string datetime = customerIdal.getTime();
//插入上机表
lineEntity.cardID = cardID;
lineEntity.cName = cName;
lineEntity.lineState = Constant.ONLINE;
lineEntity.onDateTime = Convert.ToDateTime(datetime);
lineEntity.type = cType;
lineEntity.computer = Environment.MachineName;
LineIDAL lineIDAL = lineFactory.line();
lineIDAL.insertOnLine(lineEntity);
//把数据回显给U层
hashtable.Add("cType", cType);
hashtable.Add("cCash", cCash);
hashtable.Add("cName", cName);
hashtable.Add("rate", rate);
hashtable.Add("datetime", datetime);
return hashtable;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
用于卡号不存在的情况
/// <summary>
/// 该类用于卡号不存在的情况
/// </summary>
public class OnLineIsCardExit:OnLineHandler
{
public override Hashtable HandleRequest(string cardID)
{
CustomerIDAL customerIDAL = customerFactory.Customer();
List<CustomerEntity> listCustomer = customerIDAL.getCustomerByCardID(cardID);
if (listCustomer.Count == 0)
{
throw new Exception("该卡号不存在");
}
else
{
return successor.HandleRequest(cardID);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
处理卡号已经上机的情况
/// <summary>
/// 该类用于处理卡号已经上机的情况
/// </summary>
public class OnLineIsOnLine:OnLineHandler
{
public override Hashtable HandleRequest(string cardID)
{
LineIDAL lineIDAL = lineFactory.line();
List<LineEntity> listLine = lineIDAL.getLastLineByCardID(cardID);
//没有上机记录就直接进入下一个职责的判断
if (listLine.Count==0)
{
return successor.HandleRequest(cardID);
}
if (listLine[0].lineState.Trim() == Constant.ONLINE)
{
//该卡正在上机
throw new Exception("暂时还不可以上机,因为该卡正在上机");
}
else
{
return successor.HandleRequest(cardID);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
余额不足的情况
/// <summary>
/// 该类用于余额不足上机情况的处理
/// </summary>
public class OnLineLimitCash : OnLineHandler
{
public override Hashtable HandleRequest(string cardID)
{
CustomerIDAL customerIdal = customerFactory.Customer();
List<CustomerEntity> listCustomer = customerIdal.getCustomerByCardID(cardID);
BasicdataIDAL basicdataIDAL = basicdataFactory.BasicData();
List<BasicdataEntity> listBasicdata = basicdataIDAL.getBasicdataInfo();
if (listCustomer[0].cCash < listBasicdata[0].limitCash)
{
throw new Exception("余额不足,无法上机,请去前台充值");
}
else
{
return successor.HandleRequest(cardID);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
文章来源: blog.csdn.net,作者:张艳伟_Laura,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/Laura__zhang/article/details/114317282
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)