频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

Java敏捷开发技巧之消除代码异味

来源:赛迪网 作者:yongbing 出处:巧巧读书 2007-12-28 进入讨论组
上一页 1 2 3 4 5 

但问题是,三种子类的行为(里面的代码)都差不多一样,getPasswordMaxAgeInDays这个方法就一个数值不同(30,90或者Integer.MAX_VALUE)。canPrintReport这个方法也不同在一个数值(true或false)。这三种用户类型只需要用三个对象代替就行了,无须特别地新建三个子类了:

class UserAccount {
UserType userType;
String id; 
String name;
String password;
Date dateOfLastPasswdChange;
UserType getType() {
return userType;
}  

class UserType {
int passwordMaxAgeInDays;
boolean allowedToPrintReport;
UserType(int passwordMaxAgeInDays, boolean allowedToPrintReport) {
this.passwordMaxAgeInDays = passwordMaxAgeInDays;
this.allowedToPrintReport = allowedToPrintReport;
}  
int getPasswordMaxAgeInDays() {
return passwordMaxAgeInDays;
}  
boolean canPrintReport() {
return allowedToPrintReport;
}  
static UserType normalUserType = new UserType(90, true);
static UserType adminUserType = new UserType(30, true);
static UserType guestUserType = new UserType(Integer.MAX_VALUE, false);

class InventoryApp {
void login(UserAccount userLoggingIn, String password) {
if (userLoggingIn.checkPassword(password)) {
GregorianCalendar today = new GregorianCalendar();
GregorianCalendar expiryDate = getAccountExpiryDate(userLoggingIn);
if (today.after(expiryDate)) {
//提示用户修改密码
...
}  
}  
}  
GregorianCalendar getAccountExpiryDate(UserAccount account) {
int passwordMaxAgeInDays = getPasswordMaxAgeInDays(account);
GregorianCalendar expiryDate = new GregorianCalendar();
expiryDate.setTime(account.dateOfLastPasswdChange);
expiryDate.add(Calendar.DAY_OF_MONTH, passwordMaxAgeInDays);
return expiryDate;
}  
int getPasswordMaxAgeInDays(UserAccount account) {
return account.getType().getPasswordMaxAgeInDays();
}  
void printReport(UserAccount currentUser) {
boolean canPrint;
canPrint = currentUser.getType().canPrintReport();
if (!canPrint) {
throw new SecurityException("You have no right");
}  
//打印报表.
}  
}
 
注意到了吧!用一个对象代替类别,同样可以移除switch或者if-then-else-if。  

总结一下类别代码的移除

要移动一些类别代码和switch表达式,有两种方法: 

1、用基于同一父类的不同子类来代替不同的类别。
2、用一个类的不同对象来代替不同的类别。

当不同的类别具有比较多不同的行为时,用第一种方法。当这些类别的行为非常相似,或者只是差别在一些值上面的时候,用第二个方法。

普遍的代码异味

类别代码和switch表达式是比较普遍的代码异味。此外,还有其他的代码异味也很普遍。

下面是大概的异味列表:

◆代码重复
◆太多的注释
◆类别代码(type code)
◆switch或者一大串if-then-else-if
◆想给一个变量,方法或者类名取个好名字时,也怎么也取不好
◆用类似XXXUtil, XXXManager, XXXController 和其他的一些命名
◆在变量,方法或类名中使用这些单词“And”,“Or”等等  
◆一些实例中的变量有时有用,有时没用
◆一个方法的代码太多,或者说方法太长
◆一个类的代码太多,或者说类太长
◆一个方法有太多参数
◆两个类都引用了彼此(依赖于彼此)

浏览URL http://www.qqread.com/java/2007/12/w389616.html 更多文章 更多内容请看Office技巧专区Java环境安装配置网管实用技巧100例专题,或进入讨论组讨论。
上一页 1 2 3 4 5 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章