refactoring--除去代码异味(bad smell)(2)
来源:互联网 发布:醍醐灌顶的一句话 知乎 编辑:程序博客网 时间:2024/04/25 01:42
让我们来看一下另外一个例子 ,在当前的系统中 ,有三种用户 :常规用户 ,管理员和游客 。
常规用户必须每隔90 天修改一次密码 (更频繁也行 ),管理员必须每30 天修改一次密码 ,游客就不需要修改了,常规用户跟管理员可以打印报表 。
先看一下当前的代码 :
class UserAccount {
final static int USERTYPE_NORMAL = 0;
final static int USERTYPE_ADMIN = 1;
final static int USERTYPE_GUEST = 2;
int userType;
String id;
String name;
String password;
Date dateOfLastPasswdChange;
public boolean checkPassword(String password) {
...
}
}
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) {
switch (account.getType()) {
case UserAccount.USERTYPE_NORMAL:
return 90;
case UserAccount.USERTYPE_ADMIN:
return 30;
case UserAccount.USERTYPE_GUEST:
return Integer.MAX_VALUE;
}
}
void printReport(UserAccount currentUser) {
boolean canPrint;
switch (currentUser.getType()) {
case UserAccount.USERTYPE_NORMAL:
canPrint = true;
break;
case UserAccount.USERTYPE_ADMIN:
canPrint = true;
break;
case UserAccount.USERTYPE_GUEST:
canPrint = false;
}
if (!canPrint) {
throw new SecurityException("You have no right");
}
//打印报表
}
}
用一个对象代替一种类别 (注意 ,之前是一个类代替一种类别 )
根据之前讲的解决方法 ,要去掉类别代码 ,我们只需要为每种类别创建一个子类 ,比如:
abstract class UserAccount {
String id;
String name;
String password;
Date dateOfLastPasswdChange;
abstract int getPasswordMaxAgeInDays();
abstract boolean canPrintReport();
}
class NormalUserAccount extends UserAccount {
int getPasswordMaxAgeInDays() {
return 90;
}
boolean canPrintReport() {
return true;
}
}
class AdminUserAccount extends UserAccount {
int getPasswordMaxAgeInDays() {
return 30;
}
boolean canPrintReport() {
return true;
}
}
class GuestUserAccount extends UserAccount {
int getPasswordMaxAgeInDays() {
return Integer.MAX_VALUE;
}
boolean canPrintReport() {
return false;
}
}
但问题是 ,三种子类的行为 (里面的代码 )都差不多一样,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”等等
一些实例中的变量有时有用,有时没用
一个方法的代码太多,或者说方法太长
一个类的代码太多,或者说类太长
一个方法有太多参数
两个类都引用了彼此(依赖于彼此)
- refactoring--除去代码异味(bad smell)(2)
- refactoring--除去代码异味(bad smell)(1)
- Bad Smell(代码的坏味道)
- Bad Smell(代码的坏味道)
- 代码的坏味道(bad smell)
- Bad Smell(代码的坏味道)
- 什么样的代码是坏代码,是有异味(smell)的代码?
- 使用java 动态代理去掉代码中的bad smell。
- 重构 之代码的坏味道(Bad smell)
- 重构 之代码的坏味道(Bad smell)
- Bad smell in code (代码的坏味道)
- code-smell-2-重复代码
- Bad Smell Code
- 《代码之美》第7章 漂亮的测试 的bad smell
- 当感觉自己写的代码表达不够清晰,有些bad smell时应该怎么做?
- 清除代码异味
- 清除代码异味
- 清除代码异味
- hibernate 操作
- refactoring--除去代码异味(bad smell)(1)
- C#编程规范(1)
- m2和wmo模型导入插件更新
- C#编程规范(2)
- refactoring--除去代码异味(bad smell)(2)
- 2006年12月18日(bbs系统)
- CSS属性列表
- 改进后的多线程文件搜索
- 行业软件英文缩写中文翻译
- 关于矩阵旋转算法的实现
- IIS6的SMTP
- 调试日志-不匹配的消息映射宏和相应函数的错误。
- KDE的MacMode