标签:
int basePrice = _quantity * _itemPrice; int discountLevel; if (_quantity > 100) discountLevel = 2; else discountLevel = 1; double finalPrice = discountedPrice (basePrice, discountLevel); return finalPrice; } private double discountedPrice (int basePrice, int discountLevel) { if (discountLevel == 2) return basePrice * 0.1; else return basePrice * 0.05; }
return discountedPrice (); } private double discountedPrice () { if (getDiscountLevel() == 2) return getBasePrice() * 0.1; else return getBasePrice() * 0.05; } private double getBasePrice() { return _quantity * _itemPrice; }
class Room... boolean withinPlan(HeatingPlan plan) { int low = daysTempRange().getLow(); int high = daysTempRange().getHigh(); return plan.withinRange(low, high); } class HeatingPlan... boolean withinRange (int low, int high) { return (low >= _range.getLow() && high <= _range.getHigh()); } private TempRange _range;
boolean withinRange (TempRange roomRange) { return (roomRange.getLow() >= _range.getLow() && roomRange.getHigh() <= _range.getHigh()); } class Room... boolean withinPlan(HeatingPlan plan) { int low = daysTempRange().getLow(); int high = daysTempRange().getHigh(); return plan.withinRange(daysTempRange()); }
boolean withinRange (TempRange roomRange) { return (_range.includes(roomRange)); } class TempRange... boolean includes (TempRange arg) { return arg.getLow() >= this.getLow() && arg.getHigh() <= this.getHigh();
double getFlowBetween (Date start, Date end) { double result = 0; Enumeration e = _entries.elements(); while (e.hasMoreElements()) { Entry each = (Entry) e.nextElement(); if (each.getDate().equals(start) || each.getDate().equals(end) || (each.getDate().after(start) && each.getDate().before(end))) { result += each.getValue(); } } return result; } private Vector _entries = new Vector(); client code...
DateRange (Date start, Date end) { _start = start; _end = end; } Date getStart() { return _start; } Date getEnd() { return _end; } private final Date _start; private final Date _end; }
double getFlowBetween (DateRange range) { double result = 0; Enumeration e = _entries.elements(); while (e.hasMoreElements()) { Entry each = (Entry) e.nextElement(); if (range.includes(each.getDate())) { result += each.getValue(); } } return result; } class DateRange... boolean includes (Date arg) { return (arg.equals(_start) || arg.equals(_end) || (arg.after(_start) && arg.before(_end))); }
参考资料:
https://sourcemaking.com/refactoring/preserve-whole-object
https://sourcemaking.com/refactoring/replace-parameter-with-method
https://sourcemaking.com/refactoring/introduce-parameter-object
Long Parameter List(过长参数列)---要重构的味道
标签:
原文地址:http://www.cnblogs.com/ttylinux/p/4575307.html