《Java重构示例五(共9页).docx》由会员分享,可在线阅读,更多相关《Java重构示例五(共9页).docx(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上Java重构示例五关键字:Java 程序设计 重构 示例 技巧 原则 优化 方法序言本文通过Java示例代码片段展示了常用重构原则和技巧,供初级开发人员参考。精致的代码能够清楚传达作者的意图,精致的代码是最好的注释,精致的代码非常容易维护和扩展。程序员阅读精致的代码如同大众欣赏优美的散文一样享受。21 使用类替换类型代码21.1 重构前public class LabelComparator implements Comparator, Serializable private static final long serialVersionUID = 1L; publ
2、ic static final int ASC = 1; public static final int DESC = 2; private int sortType = ASC; public LabelComparator() public LabelComparator(int sortType) this.sortType = sortType; public int compare(Object o1, Object o2) if (o1 = null & o2 = null) return 0; if (o1 = null) return -1; if (o2 = null) re
3、turn -1; if (Label) o1).getIndex() (Label) o2).getIndex() return (sortType = ASC) ? 1 : -1; else return 0; 21.2 重构后public final class SortMode implements Serializable private static final long serialVersionUID = 1L; private static final Map INSTANCES = new HashMap(); private final int type; private
4、final String name; private SortMode(int type, String name) this.type = type; this.name = name; public String toString() return name; public static final SortMode ASC = new SortMode(1, ASC); public static final SortMode DESC = new SortMode(2, DESC); static INSTANCES.put(ASC.name, ASC); INSTANCES.put(
5、DESC.name, DESC); public boolean isAsc() return ASC.type = this.type; public boolean isDesc() return DESC.type = this.type; private Object readResolve() return INSTANCES.get(name); public static SortMode parse(String name) return (SortMode) INSTANCES.get(name); public boolean equals(Object obj) if (
6、obj instanceof SortMode) SortMode that = (SortMode) obj; if (that.type = this.type) return true; return false; else return false; public class LabelComparator implements Comparator, Serializable private static final long serialVersionUID = 1L; public SortMode mode = SortMode.ASC; public LabelCompara
7、tor() public LabelComparator(SortMode mode) this.mode = mode; public int compare(Object o1, Object o2) if (o1 = null & o2 = null) return 0; if (o1 = null) return -1; if (o2 = null) return -1; if (Label) o1).getIndex() (Label) o2).getIndex() return mode.isAsc() ? 1 : -1; else return 0; 22 使用对象封装参数22.
8、1 重构前public int getRemainMinutes(int hour, int minute, int fromHour, int fromMinute int toHour, int toMinute) / -from-to-position- int startHour = toHour; int startMinute = toMinute; if (this.fromAfterEqual(hour, minute) / -position-from-to- startHour = fromHour; startMinute = fromMinute; else if (t
9、his.toAfterEqual(hour, minute) / -from-position-to- startHour = hour; startMinute = minute; return this.getMinutes(startHour, startMinute, toHour, toMinute);22.2 重构后public class DayPart implements Serializable int fromHour = -1; int fromMinute = -1; int toHour = -1; int toMinute = -1; public int get
10、FromHour() return fromHour; public void setFromHour(int fromHour) this.fromHour = fromHour; public int getFromMinute() return fromMinute; public void setFromMinute(int fromMinute) this.fromMinute = fromMinute; public int getToHour() return toHour; public void setToHour(int toHour) this.toHour = toHo
11、ur; public int getToMinute() return toMinute; public void setToMinute(int toMinute) this.toMinute = toMinute; public int getRemainMinutes(int hour, int minute, DatePart datePart) int fromHour = datePart.getFromHour(); int fromMinute = datePart.getFromMinute(); int toHour = datePart.getToHour(); int
12、toMinute = datePart.getToMinute(); / -from-to-position- int startHour = toHour; int startMinute = toMinute; if (this.fromAfterEqual(hour, minute) / -position-from-to- startHour = fromHour; startMinute = fromMinute; else if (this.toAfterEqual(hour, minute) / -from-position-to- startHour = hour; start
13、Minute = minute; return this.getMinutes(startHour, startMinute, toHour, toMinute);23 封装集合操作23.1 重构前public Class Group private List userList = new ArrayList(); public void setUserList(List userList) this.userList = userList; public List getUserList() return this.userList; 23.2 重构后public Class Group p
14、rivate List userList = new ArrayList(); public void setUserList(List userList) this.userList = userList; public List getUserList() return this.userList; public void addUser(User user) this.getUserList().add(user); user.setGroup(this); public void removeUser(User user) this.getUserList().remove(user)
15、; user.setGroup(null); 24 避免一次性临时变量24.1 重构前public int countWeekDay(Month month, WeekDay weekDay) int count = 0; int weeks = this.getDates()month.getMonth(); for (int week = 0, weekLen = weeks.length; week 0) count+; return count;24.2 重构后public int countWeekDay(Month month, WeekDay weekDay) int count
16、 = 0; int weeks = this.getDates()month.getMonth(); for (int week = 0, weekLen = weeks.length; week 0) count+; return count;25 一个变量一种作用25.1 重构前public IPolyDate getIndexWeekDay(Month month, int index, WeekDay weekDay) int count = this.countWeekDay(month, weekDay); if (index count) throw new ExceedMaxW
17、eekIndexOfMonthException(Arguement index + index + exceeds max week index + count + of month + month.toString() + .); count = 0; int weeks = this.getDates()month.getMonth(); for (int week = 0, weekLen = weeks.length; week 0) if (+count = index) return new PolyDate(year, month.getMonth(), date); retu
18、rn null;25.2 重构后public IPolyDate getIndexWeekDay(Month month, int index, WeekDay weekDay) int maxCountOfWeekDay = this.countWeekDay(month, weekDay); if (index maxCountOfWeekDay) throw new ExceedMaxWeekIndexOfMonthException(Arguement index + index + exceeds max week index+ maxCountOfWeekDay + of month + month.toString() + .); int count = 0; int weeks = this.getDates()month.getMonth(); for (int week = 0, weekLen = weeks.length; week 0) if (+count = index) return new PolyDate(year, month.getMonth(), date); return null;专心-专注-专业