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

B层

 public class ChainBLL
    {
        public void  inquiryBasicInfo()
        {
            //调用工厂方法创建接口
            Factory.BasicDataFactory fact = new Factory.BasicDataFactory();
            //调用接口
            IDAL.BasicDataIDAL idal = fact.InquiryBasicData();
            //调用D层查询方法
            DataTable table = idal.inquiryBasicData();
            rate = Convert.ToDecimal ( table.Rows[0]["Rate"].ToString ());                      
        }
        public static  decimal rate;
        public  static  decimal cash;
        
        public decimal ConsumeCash(int time)
        {
            inquiryBasicInfo();
            ChainMoney c1 = new ConcreChainMoney1();
            ChainMoney c2 = new ConcreChainMoney2();
            ChainMoney c3 = new ConcreChainMoney3();
            c1.SetSuccessor(c2);
            c2.SetSuccessor(c3);
            c1.consumecash(time);            
            return cash;
        }
        public  abstract class ChainMoney
        {
            protected ChainMoney successor;//设置继任者
            public void SetSuccessor(ChainMoney successor)
            {
                this.successor = successor;
            }
            public abstract void consumecash(int time);//处理计算时间的方法

            
        }
        //当时间在0-2分钟之内则有权处理,否则转到下一位
        public  class ConcreChainMoney1 : ChainMoney
        {
            public override void consumecash(int time)
            {
                if (time>=0 &&time <2)//时间开始到准备时间时,消费金额为0
                {
                    cash  = 0;
                }
                else if (successor !=null)
                
                {
                    successor.consumecash(time);//转到下一位
                }
            }
        }
        //当时间在2分钟到1小时内则有权处理,否则转到下一位
        public  class ConcreChainMoney2 : ChainMoney
        {
            public override void consumecash(int time)//时间为2分钟到1小时之内,消费金额为1.2倍
            {
                if (time >=2 && time <60)
                {
                    cash =  rate/ 60 * time*Convert.ToDecimal(1.2);
                }
                else if (successor!=null  )
                {
                    successor.consumecash(time);//移到下一位
                }
            }
        }
        //当时间大于1小时则有权处理,否则转到下一位
        public  class ConcreChainMoney3:ChainMoney 
        {
            public override void consumecash(int time)//时间为1小时之外,消费金额按正常计算
            {
                if (time>=60)
                {
                    cash = rate / 60 * time;
                }
                else if (successor !=null)
                {
                    successor.consumecash(time);
                }
            }
        }
    }

Facade层

public  class ChainFacade
    {
        public decimal ConsumeCash(int time)//计算金额
        {
            BLL.ChainBLL chainBLL = new BLL.ChainBLL();
            return chainBLL.ConsumeCash(time);
        }
    }

U层

 public void Consumecash()//根据消费时间计算消费金钱
        {
           
            Facade.ChainFacade faca = new Facade.ChainFacade(); 
            time = TimeCount.Minutes;
            consumecash = faca.ConsumeCash(time);
            lblConsumeCash.Text = consumecash.ToString();
        }

你可能感兴趣的:(——C#版机房收费系统)