版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、Java重構示例五關鍵字:Java 程序設計 重構 示例 技巧 原則 優(yōu)化 方法序言本文通過Java示例代碼片段展示了常用重構原則和技巧,供初級開發(fā)人員參考。精致的代碼能夠清楚傳達作者的意圖,精致的代碼是最好的注釋,精致的代碼非常容易維護和擴展。程序員閱讀精致的代碼如同大眾欣賞優(yōu)美的散文一樣享受。21 使用類替換類型代碼21.1 重構前public class LabelComparator implements Comparator, Serializable private static final long serialVersionUID = 1L; public static fin
2、al 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) return -1; if (
3、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 final String
4、name; private SortMode(int type, String name) this.type = type; = 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(DESC.name, DE
5、SC); 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 (obj instanceo
6、f 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 LabelComparator() public
7、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 使用對象封裝參數(shù)22.1 重構前public i
8、nt 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 (this.toAfterEq
9、ual(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 getFromHour() re
10、turn 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 = toHour; public in
11、t 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 toMinute = da
12、tePart.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; startMinute = minu
13、te; 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 private List u
14、serList = 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); user.setGro
15、up(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 = 0; int wee
16、ks = 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 ExceedMaxWeekIndexOfMon
17、thException(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); return 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 + i
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年醫(yī)學專家知識保護協(xié)議
- 2025年農(nóng)村廢棄民房購買合同
- 2025年分期付款購買裝修家具協(xié)議
- 2025年代理商業(yè)務保密協(xié)議
- 2025年奢侈品銷售代理合作合同
- 2025年室內(nèi)裝飾施工驗收設計協(xié)議
- 2025年度定制化母嬰護理月嫂服務合同4篇
- 高空設施安裝與拆除作業(yè)安全協(xié)議書3篇
- 2025版大學食堂冷鏈食材配送服務合同模板3篇
- 2025版土地證抵押個人借款合同示范文本3篇
- 2025屆高考英語 716個閱讀理解高頻詞清單
- 報建協(xié)議書模板
- 汽車配件購銷合同范文
- 貴州省2024年中考英語真題(含答案)
- 施工項目平移合同范本
- (高清版)JTGT 3360-01-2018 公路橋梁抗風設計規(guī)范
- 胰島素注射的護理
- 云南省普通高中學生綜合素質(zhì)評價-基本素質(zhì)評價表
- 2024年消防產(chǎn)品項目營銷策劃方案
- 聞道課件播放器
- 五星級酒店收入測算f
評論
0/150
提交評論