반응형
before
public class ReplaceParameterWithMethodCall_before {
static public int quantity;
static public int itemPrice;
static public double getPrice() {
int basePrice = quantity * itemPrice;
int discountLevel;
if (quantity > 100)
discountLevel = 2;
else
discountLevel = 1;
double finalPrice = discountedPrice(basePrice, discountLevel);
return finalPrice;
}
static private double discountedPrice(int basePrice, int discountLevel) {
if (discountLevel == 2)
return basePrice * 0.1;
else
return basePrice * 0.05;
}
public static void main(String[] args) {
quantity = 10;
itemPrice = 25;
System.out.println("price : " + getPrice());
}
}
after
import java.util.ArrayList;
import java.util.Random;
public class IntroduceParameterObject_before {
static class Account {
//...
private ArrayList<Integer> _entries = new ArrayList<Integer>();
public Account() {
Random rand = new Random();
int numbers = rand.nextInt(50);
for(int i = 0; i < numbers; i++) {
_entries.add(rand.nextInt(50));
}
printAccount();
}
public double getFlowBetween (Bound bound) {
double result = 0;
for (int each : _entries) {
if (bound.start <= each && each <= bound.end) {
result += each;
}
}
return result;
}
public void printAccount() {
System.out.println(_entries.size());
for(int num : _entries) {
System.out.printf("%d ", num);
}
System.out.println();
}
}
public static class Bound {
int start;
int end;
public Bound(int start, int end) {
this.start = start;
this.end = end;
}
}
public static void main(String[] args) {
Bound bound = new Bound(15, 25);
Account anAccount = new Account();
double flow = anAccount.getFlowBetween(bound);
System.out.println(flow);
}
}
반응형
'Design Architecture > Refactoring' 카테고리의 다른 글
Replace Parameter with Explicit Methods (0) | 2022.04.20 |
---|---|
Replace Parameter with Method Call (0) | 2022.04.20 |
Separate Query From Modifier (0) | 2022.04.20 |
Replace Nested Conditional With Guard Clauses (0) | 2022.04.20 |
Decompose Conditional (0) | 2022.04.20 |