标签:
public class OrderLineItem {
public Double Price;
}
public class Order {
private List<OrderLineItem> OrderLineItems;
private List<Double> Discounts;
private Double Tax;
public Double Calculate() {
Double subTotal = 0d;
// Total up line items
for(OrderLineItem lineItem : OrderLineItems) {
subTotal += lineItem.Price;
}
// Subtract Discounts
for(Double discount : Discounts) subTotal -= discount;
// Calculate Tax
Double tax = subTotal * Tax;
// Calculate GrandTotal
Double grandTotal = subTotal + tax;
return grandTotal;
}
}
public class OrderLineItem{
public Double Price;
}
public class Order {
public List<OrderLineItem> OrderLineItems;
public List<Double> Discounts;
public Double Tax;
public Double Calculate() {
return new OrderCalculator(this).Calculate();
}
}
public class OrderCalculator {
private Double SubTotal;
private List<OrderLineItem> OrderLineItems;
private List<Double> Discounts;
private Double Tax;
public OrderCalculator(Order order) {
OrderLineItems = order.OrderLineItems;
Discounts = order.Discounts;
Tax = order.Tax;
}
public Double Calculate() {
CalculateSubTotal();
SubtractDiscounts();
CalculateTax();
return SubTotal;
}
private void CalculateSubTotal() {
// Total up line items
for(OrderLineItem lineItem : OrderLineItems) SubTotal += lineItem.Price;
}
private void SubtractDiscounts() {
// Subtract Discounts
for(Double discount : Discounts) SubTotal -= discount;
}
private void CalculateTax() {
// Calculate Tax
SubTotal += SubTotal * Tax;
}
}
重构13-Extract Method Object(提取方法对象)
标签:
原文地址:http://www.cnblogs.com/jgig11/p/5786329.html