C#机房重构—下机之职责链模式

【前言】

说到设计模式,又熟悉又陌生,为什么这么说呢?熟悉是因为学过设计模式,明白了当时学习的例子;陌生是因为放到重构不会用,参考了很多博客,才知道如何用这个职责链模式。

【内容】

BLL层

没有设计准备时间,上机满一分钟即收费。

 public class ChainBLL
 {
    public static decimal rate;
    public static decimal tmprate;
    public static decimal cash;

    //查询基础表
    public void inquiryBasicInfo() 
    {
        //调用工厂方法创建接口
        Factory.OutLineFactory outlinefactory = new Factory.OutLineFactory();
        //调用接口
        IDAL.OutLineIDAL idal = outlinefactory.OutLine();
        Entity.BasicDate basicdate = new Entity.BasicDate();

        //调用D层查询方法
        DataTable table = idal.SelectBasic(basicdate);

        tmprate = Convert.ToDecimal(table.Rows[0]["tmpRate"].ToString());
        rate = Convert.ToDecimal(table.Rows[0]["Rate"].ToString());
                       
    }
       
    public decimal ConsumeCash(double time ,string type)
    {
        inquiryBasicInfo();
        ChainMoney c1 = new ConcreChainMoney1();
        ChainMoney c2 = new ConcreChainMoney2();
        c1.SetSuccessor(c2);
        c1.consumecash(time,type);
        return cash;
    }

    // 定义一个抽象类,来处理各个请求
    public abstract class ChainMoney  
    {
        protected ChainMoney successor;  //设置继任者
        public void SetSuccessor(ChainMoney successor)
        {
            this.successor = successor;
        }

        //处理请求计算时间的方法
        public abstract void consumecash(double time,string type);
    }

    //设置责任链上的继承者
    //当时间在1分钟到1小时内则有权处理,否则转到下一位
    public class ConcreChainMoney1 : ChainMoney
    {
        //时间为1分钟到1小时之内为临时用户
        public override void consumecash(double time,string type)
        {
            if (time >0 && time <= 60)
            {                 
                cash = Convert.ToDecimal(type);
            }
            else if (successor != null)
            {
                successor.consumecash(time,type);
            }
        }
    }

    //当时间大于1小时则有权处理,否则转到下一位
    public class ConcreChainMoney2 : ChainMoney
    {
        //时间为1小时之外,消费金额按正常计算
        public override void consumecash(double time,string type)
        {
            if (time > 60)
            {
                cash = Convert.ToDecimal(Math.Ceiling(time / 60)) * Convert.ToDecimal(type);
            }
            else if (successor != null)
            {
                successor.consumecash(time,type);
            }
        }
    }

Facade层

 public class ChainFacade
 {
    public decimal ConsumeCash(double time,string type)
    {
        BLL.ChainBLL chainBLL = new BLL.ChainBLL();
        return chainBLL.ConsumeCash(time,type);
    }
}

U层(代码未优化前)

public void ConsumeCash()
{
    Facade.ChainFacade chainfacade = new Facade.ChainFacade();
    Facade.OutLineFacade outLineFacade = new Facade.OutLineFacade();
    Entity.BasicDate basicdateInfo = new Entity.BasicDate();
    DataTable dtbasic = outLineFacade.SelectBasic(basicdateInfo);
    Entity.StudentInfo stubalance = new Entity.StudentInfo();
    stubalance.cardno = LoginUI.ID;
    DataTable dtstu = outLineFacade.SelectStuBalance(stubalance);

    //判断是普通用户还是VIP用户
    if (dtstu.Rows[0]["cardtype"].ToString() == "普通用户")
    {
         type= dtbasic.Rows[0]["TmpRate"].ToString();
    }
    else
    {
         type = dtbasic.Rows[0]["Rate"].ToString();
    }

    time = TimeCount.TotalMinutes;
    consumecash = chainfacade.ConsumeCash(time,type);
    lblConsumeCash.Text = consumecash.ToString(); 
}

 

你可能感兴趣的:(机房收费管理系统,技术成长轨迹,机房收费管理系统—个人重构版)