refactoring : long parameter list - replace parameter with query
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package com.example.refactoring._04_long_parameter_list._00_before;
|
||||
|
||||
public class Order {
|
||||
|
||||
private int quantity;
|
||||
|
||||
private double itemPrice;
|
||||
|
||||
public Order(int quantity, double itemPrice) {
|
||||
this.quantity = quantity;
|
||||
this.itemPrice = itemPrice;
|
||||
}
|
||||
|
||||
public double finalPrice() {
|
||||
double basePrice = this.quantity * this.itemPrice;
|
||||
int discountLevel = this.quantity > 100 ? 2 : 1;
|
||||
return this.discountedPrice(basePrice, discountLevel);
|
||||
}
|
||||
|
||||
private double discountedPrice(double basePrice, int discountLevel) {
|
||||
return discountLevel == 2 ? basePrice * 0.9 : basePrice * 0.95;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.example.refactoring._04_long_parameter_list._14_replace_parameter_with_query;
|
||||
|
||||
public class Order {
|
||||
|
||||
private int quantity;
|
||||
|
||||
private double itemPrice;
|
||||
|
||||
public Order(int quantity, double itemPrice) {
|
||||
this.quantity = quantity;
|
||||
this.itemPrice = itemPrice;
|
||||
}
|
||||
|
||||
public double finalPrice() {
|
||||
double basePrice = this.quantity * this.itemPrice;
|
||||
|
||||
// 함수를 호출하는 쪽의 책임을 줄인다.
|
||||
return this.discountedPrice(basePrice);
|
||||
}
|
||||
|
||||
private int getDiscountLevel() {
|
||||
return this.quantity > 100 ? 2 : 1;
|
||||
}
|
||||
|
||||
private double discountedPrice(double basePrice) {
|
||||
return getDiscountLevel() == 2 ? basePrice * 0.9 : basePrice * 0.95;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.refactoring._04_long_parameter_list._00_before;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class OrderTest {
|
||||
|
||||
@Test
|
||||
void discountedPriceWithDiscountLevel2() {
|
||||
int quantity = 200;
|
||||
double price = 100;
|
||||
assertEquals(quantity * price * 0.90, new Order(quantity, price).finalPrice());
|
||||
}
|
||||
|
||||
@Test
|
||||
void discountedPriceWithDiscountLevel1() {
|
||||
int quantity = 100;
|
||||
double price = 100;
|
||||
assertEquals(quantity * price * 0.95, new Order(quantity, price).finalPrice());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.refactoring._04_long_parameter_list._14_replace_parameter_with_query;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class OrderTest {
|
||||
|
||||
@Test
|
||||
void discountedPriceWithDiscountLevel2() {
|
||||
int quantity = 200;
|
||||
double price = 100;
|
||||
assertEquals(quantity * price * 0.90, new Order(quantity, price).finalPrice());
|
||||
}
|
||||
|
||||
@Test
|
||||
void discountedPriceWithDiscountLevel1() {
|
||||
int quantity = 100;
|
||||
double price = 100;
|
||||
assertEquals(quantity * price * 0.95, new Order(quantity, price).finalPrice());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user