一个函数的重构过程

     前天写code的时候,随手做了一个函数。要测试了,发现测这个函数有点繁琐,最重要的是我觉得的这个函数的职责太多,需要分割,所以做了重构,下面是自己的重构过程。(为了不涉及到公司的产品,下面的代码已做简化和更改,只为演示用)

      首先就功能做个说明,该函数的功能是计算一个softwaregroup的所有device使用信息,把具有相同ID的device信息进行叠加统计后输出。一个softwareGroup包括多个software, 一个software可以在多个device上安装。(红色函数为要重构函数)

   class SoftwareGroup
    {
        ArrayList GetAllDevices()
        {
            Hashtable allDevices = new Hashtable();
            ArrayList allSoftwares = this.softwares;
            foreach (Software sw in allSoftwares)
            {
                ArrayList devices = sw.GetDevices();
                foreach (Device device in devices)
                {
                    if (allDevices.ContainsKey(device.ID))
                        allDevices[device.ID] = device;
                    else
                        allDevices[device.ID] = Add(allDevices[device.ID] as Device, device);
                }
            }

            return new ArrayList(allDevices.Values);
        }

        private object Add(Device oneDevice, Device twoDevice)
        {
            Device ret = new Device();
            ret.ID = oneDevice.ID;
            ret.Name = oneDevice.Name;
            ret.Type = oneDevice.Type;
            ret.ExecNum = oneDevice.ExecNum + twoDevice.ExecNum;
            ret.LastTime = oneDevice.LastTime.CompareTo(twoDevice.LastTime) > 0 ? oneDevice.LastTime : twoDevice.LastTime;
            return ret;
        }

        ArrayList softwares;
    }

    class Software
    {
        int id;
        public ArrayList GetDevices()
        {
            return new SoftwareDAO().GetDevices(id);
        }
    }

    class Device
    {
        public int ID;
        public string Name;
        public string Type;
        public DateTime LastTime;
        public int ExecNum;
    }

    函数重构完成后:

   class SoftwareGroup
    {

        ArrayList GetAllDevices()
        {
            ArrayList devices = new ArrayList();
            foreach (Software sw in softwares)
            {
                devices.AddRange(sw.GetDevices());
            }
            return new SoftwareUtil().SumDevices(devices);
        }

    }

   class SoftwareUtil
   {
       public ArrayList SumDevices(ArrayList devices)
       {
           Hashtable allDevices = new Hashtable();
           foreach (Device device in devices)
           {
               if (allDevices.ContainsKey(device.ID))
                   allDevices[device.ID] = device;
               else
                   allDevices[device.ID] = Add(allDevices[device.ID] as Device, device);
           }
           return new ArrayList(allDevices.Values);
       }
       private object Add(Device oneDevice, Device twoDevice)
       {
           Device ret = new Device();
           ret.ID = oneDevice.ID;
           ret.Name = oneDevice.Name;
           ret.Type = oneDevice.Type;
           ret.ExecNum = oneDevice.ExecNum + twoDevice.ExecNum;
           ret.LastTime = oneDevice.LastTime.CompareTo(twoDevice.LastTime) > 0 ? oneDevice.LastTime : twoDevice.LastTime;
           return ret;
       }
   }
   

    重构后,增加了一个辅助算法类,来完成device信息叠加功能。

    通过重构得到的代码,有如下好处。首先方便了测试,重构前测试一个方法就是SoftwareGroup中的GetAllDevices,重构后,变成两个测试,一个同上,同时增加了一个SoftwareUtil中的SumDevices算法,方便了测试,主要侧重与算法的正确性。其次的好处是分清了类的职责,增加了个SoftwareUtil类,它的主要功能,就是把容器中的多个相同device的信息叠加起来,使得SoftwareGroup类的功能减少,职责更清晰。我平常写代码时,很多时候强迫让自己先写测试,然后写代码,那天因为习惯,顺手就写了代码。其实上面的重构,如果先写测试代码,就不会有这么大的重构变动了,因为如果开始写测试代码的话,会发现不容易测试,就会改变这个函数的涉及。测试驱动设计,此言不谬!

     经常看博,但很少写,语言能力差,惭愧。第一次写博,语言有点混乱,辞不达意(其中的有些过程觉的一下没法说清楚,所以偷懒省略了),敬请谅解。

     欢迎拍砖,更欢迎一起探讨!

转载于:https://www.cnblogs.com/gchhua/archive/2008/10/12/1309327.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值