版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Java重構(gòu)示例五關(guān)鍵字:Java 程序設(shè)計(jì) 重構(gòu) 示例 技巧 原則 優(yōu)化 方法序言本文通過(guò)Java示例代碼片段展示了常用重構(gòu)原則和技巧,供初級(jí)開(kāi)發(fā)人員參考。精致的代碼能夠清楚傳達(dá)作者的意圖,精致的代碼是最好的注釋?zhuān)碌拇a非常容易維護(hù)和擴(kuò)展。程序員閱讀精致的代碼如同大眾欣賞優(yōu)美的散文一樣享受。21 使用類(lèi)替換類(lèi)型代碼21.1 重構(gòu)前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 重構(gòu)后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 使用對(duì)象封裝參數(shù)22.1 重構(gòu)前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 重構(gòu)后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 重構(gòu)前public Class Group private List userList = new ArrayList(); public void setUserList(List userList) this.userList = userList; public List getUserList() return this.userList; 23.2 重構(gòu)后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 避免一次性臨時(shí)變量24.1 重構(gòu)前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 重構(gòu)后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 一個(gè)變量一種作用25.1 重構(gòu)前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 重構(gòu)后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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年醫(yī)學(xué)專(zhuān)家知識(shí)保護(hù)協(xié)議
- 2025年農(nóng)村廢棄民房購(gòu)買(mǎi)合同
- 2025年分期付款購(gòu)買(mǎi)裝修家具協(xié)議
- 2025年代理商業(yè)務(wù)保密協(xié)議
- 2025年奢侈品銷(xiāo)售代理合作合同
- 2025年室內(nèi)裝飾施工驗(yàn)收設(shè)計(jì)協(xié)議
- 2025年度定制化母嬰護(hù)理月嫂服務(wù)合同4篇
- 高空設(shè)施安裝與拆除作業(yè)安全協(xié)議書(shū)3篇
- 2025版大學(xué)食堂冷鏈?zhǔn)巢呐渌头?wù)合同模板3篇
- 2025版土地證抵押個(gè)人借款合同示范文本3篇
- 2025屆高考英語(yǔ) 716個(gè)閱讀理解高頻詞清單
- 報(bào)建協(xié)議書(shū)模板
- 汽車(chē)配件購(gòu)銷(xiāo)合同范文
- 貴州省2024年中考英語(yǔ)真題(含答案)
- 施工項(xiàng)目平移合同范本
- (高清版)JTGT 3360-01-2018 公路橋梁抗風(fēng)設(shè)計(jì)規(guī)范
- 胰島素注射的護(hù)理
- 云南省普通高中學(xué)生綜合素質(zhì)評(píng)價(jià)-基本素質(zhì)評(píng)價(jià)表
- 2024年消防產(chǎn)品項(xiàng)目營(yíng)銷(xiāo)策劃方案
- 聞道課件播放器
- 五星級(jí)酒店收入測(cè)算f
評(píng)論
0/150
提交評(píng)論