Break Responsibilities,想了半天,改为拆分职责

今天这个重构主要是讲的单一职责方面的 ,旧代码如下

    public class Video
     {
        public void PayFee(decimal fee)
         {
         }
        public void RentVideo(Video video, Customer customer)
         {
             customer.Videos.Add(video);
         }
        public decimal CalculateBalance(Customer customer)
         {
            return customer.LateFees.Sum();
         }
     }

出租碟片和顾客管理都放在一个类里面,结构不够清晰,要进行拆分,重构后代码如下

    public class Video
     {
        public void RentVideo(Video video, Customer customer)
         {
             customer.Videos.Add(video);
         }
     }
    public class Customer
     {
        public IList<decimal> LateFees { get; set; }
        public IList Videos { get; set; }
        public void PayFee(decimal fee)
         {
         }
        public decimal CalculateBalance(Customer customer)
         {
            return customer.LateFees.Sum();
         }
     }


Leave a Reply