| Version 1 (modified by chenyang, 13 years ago) (diff) |
|---|
6.21,6.28 重构lunch代码道场记实
论坛小组在5.10,5.17进行了对命名为lunch的题目对进CodeKata练习,活动效果不错,小组成员普遍觉得受匪浅.
活动下来后,秦鸿源提议,我们其实可以把lunch做得更完美,由于大家平时的工作都比较忙碌,在日常开发中,为了尽快完成工作,对代码优化与重构的重视不够,
秦鸿源的提议很不错,可以让大家互相学习重构,提升大家对代码重构的能力,大家也都非常认同这个提议,于是我们决定用一次CodeKata活动来重构lunch.
由于,5.10号活动,被其它事情打断,在5.17日,我们进行了弥补.
这次重构的主要指导思想是:如何让代码变得更容易维护.
检验的指标是,如果我在lunch中新开一个打饭的窗口,难吗?
很明显,目前的代码,是困难的,代码中有太多的难以维护的变量,和魔术数字,新开一个打饭窗口,需要修改大量的代码。
这次的重构活动,大家也是摸着石头过河,丁健勇,秦鸿源首先对有着相似的逻辑代码,进行了小规模的封装,邝巨桓提出用数组存储各种打饭的类型,这样可以用数组来遍历,避免硬代码的出现,重构后,效果还不错。
最后,陈阳提出,用枚举来代替数组,有着更好的可维护性与可读性。我们新增了LunchType.java这个枚举类,来封装打饭的类型。/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package lunch; /** * * @author pc */ public enum LunchType { Type5(5, 100, 300, 5), Type11(11, 50, 280, 3), Type13(13, 30, 150, 2), Type15(15, 15, 70, 1), Type20(20, 10, 20, 1); private int value; private int waitNum; private int totalPeople; private int handleNum; private LunchType(int value, int waitNum, int totalPeople, int handleNum){ this.value = value; this.waitNum = waitNum; this.totalPeople = totalPeople; this.handleNum = handleNum; } public static int countPepple(){ int sum = 0; for(LunchType lunchType : LunchType.values()){ sum += lunchType.getTotalPeople(); } return sum; } public int getValue() { return value; } public int getWaitNum() { return waitNum; } public int getTotalPeople() { return totalPeople; } public int getHandleNum() { return handleNum; } }
![(please configure the [header_logo] section in trac.ini)](http://www1.pconline.com.cn/hr/2009/global/images/logo.gif)