-
Notifications
You must be signed in to change notification settings - Fork 0
/
RefactoringDemo.java
123 lines (95 loc) · 3.34 KB
/
RefactoringDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
public class RefactoringDemo {
// Replace Magic Number with Symbolic Constant
private static final int MAX_ITEMS = 5;
private int[] items;
// Extract Method
private int calculateSum() {
int sum = 0;
for (int i = 0; i < MAX_ITEMS; i++) {
sum += items[i];
}
return sum;
}
// Extract Variable
private double calculateAverage() {
int sum = calculateSum();
double average = sum / (double) MAX_ITEMS;
return average;
}
// Inline Variable
private void printAverage() {
System.out.println("The average of the first " + MAX_ITEMS + " items is: " + calculateAverage());
}
// Encapsulate Field
public int[] getItems() {
return items;
}
public void setItems(int[] items) {
this.items = items;
}
public static void main(String[] args) {
RefactoringDemo demo = new RefactoringDemo();
demo.setItems(new int[] {1, 2, 3, 4, 5});
// Introduce Parameter Object
// Instead of passing multiple parameters to the method,
// create a separate class that groups the related parameters
// and pass an instance of that class to the method.
printStatistics(demo.getItems(), new StatisticsOptions(true, false));
}
// Introduce Parameter Object
private static void printStatistics(int[] items, StatisticsOptions options) {
// Extract Class
// Move related fields and methods into a separate class
// to improve the design of the code.
StatisticsCalculator calculator = new StatisticsCalculator(items, options);
if (options.isPrintSum()) {
System.out.println("Sum: " + calculator.calculateSum());
}
if (options.isPrintAverage()) {
System.out.println("Average: " + calculator.calculateAverage());
}
}
// Introduce Null Object
// Instead of returning null, create a separate class
// that represents a null value and return an instance of
// that class when a null value is required.
private static List<Integer> getItemsOrNull() {
return null;
}
private static class StatisticsCalculator {
private final int[] items;
private final boolean printSum;
private final boolean printAverage;
public StatisticsCalculator(int[] items, StatisticsOptions options) {
this.items = items;
this.printSum = options.isPrintSum();
this.printAverage = options.isPrintAverage();
}
private int calculateSum() {
int sum = 0;
for (int i : items) {
sum += i;
}
return sum;
}
private double calculateAverage() {
int sum = calculateSum();
double average = sum / (double) items.length;
return average;
}
}
private static class StatisticsOptions {
private final boolean printSum;
private final boolean printAverage;
public StatisticsOptions(boolean printSum, boolean printAverage) {
this.printSum = printSum;
this.printAverage = printAverage;
}
public boolean isPrintSum() {
return printSum;
}
public boolean isPrintAverage() {
return printAverage;
}
}
}