重构:Move Method
来源:互联网 发布:知乎费县夫妻刑警笔录 编辑:程序博客网 时间:2024/06/05 14:47
在重构:extract method篇中的,amountFor函数应该搬家到Rental类中。
public class Rental { private Movie _mMovie; private int _daysRented; public Rental(Movie movie, int daysRented) { // TODO Auto-generated constructor stub _mMovie = movie; _daysRented = daysRented; } public int getDaysRented() { return _daysRented; } public Movie getMovie() { return _mMovie; } public double getCharge() { double result = 0; switch (getMovie().getPriceCode()) { case Movie.REGULAR: result +=2; if(getDaysRented() > 2){ result +=(getDaysRented() -2) * 1.5; } break; case Movie.NEW_RELEASE: result += getDaysRented() * 3; break; case Movie.CHILDRENS: result += 1.5; if(getDaysRented() > 3){ result += (getDaysRented() - 3)* 1.5; } default: break; } return result; }}
public class Customer { private String _name; private Vector<Rental> _rentals = new Vector<Rental>();; public Customer(String name) { _name = name; } public void addRental(Rental arg) { _rentals.addElement(arg); } public String getName() { return _name; } public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration<Rental> rentals = _rentals.elements(); String result = "Rental Record For " + getName() + "\n"; while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental) rentals.nextElement(); thisAmount = amountFor(each); frequentRenterPoints++; if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) { frequentRenterPoints++; } result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n"; totalAmount += thisAmount; } result += "Amount owed is " + String.valueOf(totalAmount) + "\n"; result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points"; return result; } private double amountFor(Rental aRental) { return aRental.getCharge(); }}
方法搬家后直接调用:
public class Customer { private String _name; private Vector<Rental> _rentals = new Vector<Rental>();; public Customer(String name) { _name = name; } public void addRental(Rental arg) { _rentals.addElement(arg); } public String getName() { return _name; } public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration<Rental> rentals = _rentals.elements(); String result = "Rental Record For " + getName() + "\n"; while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental) rentals.nextElement(); thisAmount = each.getCharge(); frequentRenterPoints++; if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) { frequentRenterPoints++; } result += "\t" + each.getMovie() + "\n"; totalAmount += thisAmount; } result += "Amount owed is " + String.valueOf(totalAmount) + "\n"; result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points"; return result; }}
0 0
- 重构:Move Method
- 迁移方法(Move Method)--代码重构2
- Eclipse重构——Move Method(I)
- Eclipse重构——Move Method(III)
- Eclipse重构——Move Method(II)
- 重构--Extract Method
- 重构:Extract Method
- 重构:Inline Method
- [重构]Long Method
- 重构-Extract Method
- 重构:Extract Method
- 重构:Query Method
- Move Method(搬移函数)
- Eclipse重构——Move Field
- 重构代码--Extract Method
- 7.1 move method (搬移函数)
- Refactoring Day 2 : Move Method
- 《Refactoring》,Move Method(搬移函数)
- C++大数问题
- 复杂链表的复制
- 架构设计:系统间通信(29)——Kafka及场景应用(中2)
- <!DOCTYPE html>很重要
- POJ 2947 高斯消元法解同余方程组
- 重构:Move Method
- matlab2014a中help与参考页翻译 limit
- Android两种常见错误(ANR和FC)
- Contains Duplicate
- ACM-韩信点兵【中国剩余定理-孙子定理】
- RSA算法原理(一)
- CentOS7安装iptables防火墙
- 关于 UIImage 以及 YYImage 的内存问题与解决方案
- HDU 2037 今年暑假不AC(贪心)