| | 1 | '''6.21,6.28 重构lunch代码道场记实''' |
| | 2 | [[BR]] |
| | 3 | 论坛小组在5.10,5.17进行了对命名为lunch的题目对进CodeKata练习,活动效果不错,小组成员普遍觉得受匪浅.[[BR]] |
| | 4 | 活动下来后,秦鸿源提议,我们其实可以把lunch做得更完美,由于大家平时的工作都比较忙碌,在日常开发中,为了尽快完成工作,对代码优化与重构的重视不够,[[BR]] |
| | 5 | 秦鸿源的提议很不错,可以让大家互相学习重构,提升大家对代码重构的能力,大家也都非常认同这个提议,于是我们决定用一次CodeKata活动来重构lunch.[[BR]] |
| | 6 | 由于,5.10号活动,被其它事情打断,在5.17日,我们进行了弥补.[[BR]] |
| | 7 | 这次重构的主要指导思想是:如何让代码变得更容易维护.[[BR]] |
| | 8 | 检验的指标是,如果我在lunch中新开一个打饭的窗口,难吗?[[BR]] |
| | 9 | 很明显,目前的代码,是困难的,代码中有太多的难以维护的变量,和魔术数字,新开一个打饭窗口,需要修改大量的代码。 |
| | 10 | [[BR]] |
| | 11 | 这次的重构活动,大家也是摸着石头过河,丁健勇,秦鸿源首先对有着相似的逻辑代码,进行了小规模的封装,邝巨桓提出用数组存储各种打饭的类型,这样可以用数组来遍历,避免硬代码的出现,重构后,效果还不错。[[BR]] |
| | 12 | 最后,陈阳提出,用枚举来代替数组,有着更好的可维护性与可读性。我们新增了LunchType.java这个枚举类,来封装打饭的类型。 |
| | 13 | {{{ |
| | 14 | /* |
| | 15 | * To change this template, choose Tools | Templates |
| | 16 | * and open the template in the editor. |
| | 17 | */ |
| | 18 | package lunch; |
| | 19 | |
| | 20 | /** |
| | 21 | * |
| | 22 | * @author pc |
| | 23 | */ |
| | 24 | public enum LunchType { |
| | 25 | |
| | 26 | Type5(5, 100, 300, 5), |
| | 27 | Type11(11, 50, 280, 3), |
| | 28 | Type13(13, 30, 150, 2), |
| | 29 | Type15(15, 15, 70, 1), |
| | 30 | Type20(20, 10, 20, 1); |
| | 31 | |
| | 32 | private int value; |
| | 33 | private int waitNum; |
| | 34 | private int totalPeople; |
| | 35 | private int handleNum; |
| | 36 | |
| | 37 | private LunchType(int value, int waitNum, int totalPeople, int handleNum){ |
| | 38 | this.value = value; |
| | 39 | this.waitNum = waitNum; |
| | 40 | this.totalPeople = totalPeople; |
| | 41 | this.handleNum = handleNum; |
| | 42 | |
| | 43 | } |
| | 44 | |
| | 45 | public static int countPepple(){ |
| | 46 | int sum = 0; |
| | 47 | for(LunchType lunchType : LunchType.values()){ |
| | 48 | sum += lunchType.getTotalPeople(); |
| | 49 | } |
| | 50 | return sum; |
| | 51 | } |
| | 52 | |
| | 53 | public int getValue() { |
| | 54 | return value; |
| | 55 | } |
| | 56 | |
| | 57 | public int getWaitNum() { |
| | 58 | return waitNum; |
| | 59 | } |
| | 60 | |
| | 61 | public int getTotalPeople() { |
| | 62 | return totalPeople; |
| | 63 | } |
| | 64 | |
| | 65 | public int getHandleNum() { |
| | 66 | return handleNum; |
| | 67 | } |
| | 68 | |
| | 69 | } |
| | 70 | |
| | 71 | }}} |
| | 72 | |
| | 73 | |
| | 74 | |
| | 75 | |
| | 76 | |