欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    Java重构示例五(共9页).docx

    • 资源ID:13850921       资源大小:17.67KB        全文页数:9页
    • 资源格式: DOCX        下载积分:20金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要20金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    Java重构示例五(共9页).docx

    精选优质文档-倾情为你奉上Java重构示例五关键字:Java 程序设计 重构 示例 技巧 原则 优化 方法序言本文通过Java示例代码片段展示了常用重构原则和技巧,供初级开发人员参考。精致的代码能够清楚传达作者的意图,精致的代码是最好的注释,精致的代码非常容易维护和扩展。程序员阅读精致的代码如同大众欣赏优美的散文一样享受。21 使用类替换类型代码21.1 重构前public class LabelComparator implements Comparator, Serializable private static final long serialVersionUID = 1L; public 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) return -1; if (Label) o1).getIndex() < (Label) o2).getIndex() return (sortType = ASC) ? -1 : 1; else 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<String, SortMode> INSTANCES = new HashMap<String, SortMode>(); private final int type; private 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(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 (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 LabelComparator() 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 if (Label) o1).getIndex() > (Label) o2).getIndex() return mode.isAsc() ? 1 : -1; else return 0; 22 使用对象封装参数22.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 (this.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 getFromHour() 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 = toHour; 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 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; startMinute = minute; return this.getMinutes(startHour, startMinute, toHour, toMinute);23 封装集合操作23.1 重构前public Class Group private List<User> userList = new ArrayList<User>(); public void setUserList(List<User> userList) this.userList = userList; public List<User> getUserList() return this.userList; 23.2 重构后public Class Group private List<User> userList = new ArrayList<User>(); public void setUserList(List<User> userList) this.userList = userList; public List<User> 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.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 < weekLen; week+) int date = weeksweekweekDay.getDay(); if (date > 0) count+; return count;24.2 重构后public int countWeekDay(Month month, WeekDay weekDay) int count = 0; int weeks = this.getDates()month.getMonth(); for (int week = 0, weekLen = weeks.length; week < weekLen; week+) if (weeksweekweekDay.getDay() > 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 ExceedMaxWeekIndexOfMonthException("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 < weekLen; week+) int date = weeksweekweekDay.getDay(); if (date > 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" + 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 < weekLen; week+) int date = weeksweekweekDay.getDay(); if (date > 0) if (+count = index) return new PolyDate(year, month.getMonth(), date); return null;专心-专注-专业

    注意事项

    本文(Java重构示例五(共9页).docx)为本站会员(飞****2)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开