Java重構(gòu)示例五(同名2242)(共9頁(yè))_第1頁(yè)
Java重構(gòu)示例五(同名2242)(共9頁(yè))_第2頁(yè)
Java重構(gòu)示例五(同名2242)(共9頁(yè))_第3頁(yè)
Java重構(gòu)示例五(同名2242)(共9頁(yè))_第4頁(yè)
Java重構(gòu)示例五(同名2242)(共9頁(yè))_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論