Skip to content

Commit 5a4806c

Browse files
authored
Merge pull request iluwatar#1 from wangihzyue/master2
issue1260 change
2 parents f153421 + 1336a65 commit 5a4806c

File tree

9 files changed

+282
-49
lines changed

9 files changed

+282
-49
lines changed

compose-method/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ layout: pattern
33
title: Compose-method
44
folder: compose-method
55
permalink: /patterns/compose-method/
6-
categories: Structural
6+
categories: Idiom
77
tags:
8-
- Gang of Four
8+
- Decoupling
99
---
1010

1111
## Intent
@@ -28,7 +28,7 @@ Wikipedia says
2828
2929
**Programmatic Example**
3030

31-
Refactoring to Patterns: Simplification | Compose Method | InformIT
31+
[Refactoring to Patterns: Simplification | Compose Method | InformIT](https://www.informit.com/articles/article.aspx?p=1398607)
3232

3333
The original method is relative hard to understand the meaning.
3434
```java

compose-method/etc/compose-method.urm.puml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
@startuml
22
package com.iluwatar.composemethod {
33
class AddImpl {
4-
- DEFAULT_NUMBER : int {static}
54
+ AddImpl()
65
+ addPositiveNodesOriginal(head : Node) : int {static}
76
+ addPositiveNodesRefined(head : Node) : int {static}

compose-method/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,5 @@
2727
</plugin>
2828
</plugins>
2929
</build>
30-
<properties>
31-
<maven.compiler.source>11</maven.compiler.source>
32-
<maven.compiler.target>11</maven.compiler.target>
33-
</properties>
3430

3531
</project>

compose-method/src/main/java/com/iluwatar/composemethod/AddImpl.java

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
/**
44
* implement adding.
55
*/
6-
public class AddImpl {
6+
public class AddImpl { //NOPMD
7+
/**
8+
* default added number.
9+
*/
710
private static final int DEFAULT_NUMBER = 0;
811

912
/**
@@ -12,7 +15,7 @@ public class AddImpl {
1215
* @param head the head of the list.
1316
* @return the sum of positive numbers.
1417
*/
15-
public static int addPositiveNodesOriginal(Node head) {
18+
public static int addPositiveNodesOriginal(final Node head) {
1619
Node tail = head;
1720
int rtn = 0;
1821
while (tail != null) {
@@ -34,7 +37,7 @@ public static int addPositiveNodesOriginal(Node head) {
3437
* @param head the head of the list.
3538
* @return the sum of the positive number.
3639
*/
37-
public static int addPositiveNodesRefined(Node head) {
40+
public static int addPositiveNodesRefined(final Node head) {
3841
Node tail = head;
3942
int rtn = 0;
4043
while (nodeDetect(tail)) {
@@ -50,7 +53,7 @@ public static int addPositiveNodesRefined(Node head) {
5053
* @param tail the current node.
5154
* @return the next node of the current node.
5255
*/
53-
public static Node nextNode(Node tail) {
56+
public static Node nextNode(final Node tail) {
5457
return tail.next;
5558
}
5659

@@ -60,18 +63,33 @@ public static Node nextNode(Node tail) {
6063
* @param tail the current node.
6164
* @return the added result.
6265
*/
63-
public static int numberGrowth(Node tail) {
64-
if (!lengthDetect(tail)) {
65-
return DEFAULT_NUMBER;
66+
public static int numberGrowth(final Node tail) {
67+
int result;
68+
if (lengthDetect(tail)) {
69+
result = grow(tail.nums); //NOPMD
70+
} else {
71+
result = DEFAULT_NUMBER; //NOPMD
6672
}
67-
return grow(tail.nums);
73+
return result;
6874
}
6975

70-
public static boolean nodeDetect(Node tail) {
76+
/**
77+
* check whether the node is null.
78+
*
79+
* @param tail current node.
80+
* @return whether it is null.
81+
*/
82+
public static boolean nodeDetect(final Node tail) {
7183
return tail != null;
7284
}
7385

74-
public static boolean lengthDetect(Node tail) {
86+
/**
87+
* check the length of list is positive.
88+
*
89+
* @param tail the current node.
90+
* @return whether its list is positive.
91+
*/
92+
public static boolean lengthDetect(final Node tail) {
7593
return tail.nums.length > 0;
7694
}
7795

@@ -81,10 +99,11 @@ public static boolean lengthDetect(Node tail) {
8199
* @param nums the list of numbers.
82100
* @return the sum of the numbers.
83101
*/
84-
public static int grow(int[] nums) {
102+
public static int grow(int[] nums) { //NOPMD
85103
int rtn = 0;
86-
for (int i = 0; i < nums.length; i++) {
87-
rtn += numGrow(nums[i]);
104+
for (final int x :
105+
nums) {
106+
rtn += numGrow(x);
88107
}
89108
return rtn;
90109
}
@@ -95,14 +114,23 @@ public static int grow(int[] nums) {
95114
* @param adder the number for judgement.
96115
* @return if number is positive, return it. Otherwise, return default number.
97116
*/
98-
public static int numGrow(int adder) {
117+
public static int numGrow(final int adder) {
118+
int result;
99119
if (isPositive(adder)) {
100-
return adder;
120+
result = adder;
121+
} else {
122+
result = DEFAULT_NUMBER;
101123
}
102-
return DEFAULT_NUMBER;
124+
return result;
103125
}
104126

105-
public static boolean isPositive(int a) {
106-
return a > 0;
127+
/**
128+
* check the number is positive.
129+
*
130+
* @param adder the number.
131+
* @return whethr is positive.
132+
*/
133+
public static boolean isPositive(final int adder) {
134+
return adder > 0;
107135
}
108136
}

compose-method/src/main/java/com/iluwatar/composemethod/App.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
package com.iluwatar.composemethod;
22

3+
import java.security.SecureRandom;
34
import java.util.Random;
45
import lombok.extern.slf4j.Slf4j;
56

67
/**
7-
* add original method is rough, and the new add method is using the composed method.
8-
* {@link Node} is the node for list, and this term is to add the positive integer in
8+
* add original method is rough, and the new
9+
* add method is using the composed method.
10+
* In this example, refactor the method to
11+
* let each method contain only one function
12+
* and ensure the refactored method's result
13+
* is equals to the original method.
14+
* {@link Node} is the node for list, and
15+
* this term is to add the positive integer in
916
* each node's integer list
1017
*/
1118
@Slf4j
12-
public class App {
19+
public class App { //NOPMD
1320
/**
1421
* Program entry point.
1522
*
1623
* @param args command line args.
1724
*/
18-
public static void main(String[] args) {
25+
public static void main(final String[] args) {
1926
//initialize a list.
20-
Node head = new Node();
27+
final var head = new Node();
2128
LOGGER.info("head: {}", head);
22-
Random random = new Random();
23-
int nodes = random.nextInt(10);
24-
Node tail = head;
29+
final SecureRandom random = new SecureRandom();
30+
final int nodes = random.nextInt(10);
31+
var tail = head; //NOPMD
2532

2633
//for each node to initialize the integer list.
2734
head.nums = new int[random.nextInt(10)];
2835
for (int j = 0; j < head.nums.length; j++) {
2936
head.nums[j] = random.nextInt(20) - 10;
3037
}
3138
for (int i = 0; i < nodes; i++) {
32-
tail.next = new Node();
39+
tail.next = new Node(); //NOPMD
3340
tail = tail.next;
34-
tail.nums = new int[random.nextInt(10)];
41+
tail.nums = new int[random.nextInt(10)]; //NOPMD
3542
for (int j = 0; j < tail.nums.length; j++) {
3643
tail.nums[j] = random.nextInt(20) - 10;
3744
}

compose-method/src/main/java/com/iluwatar/composemethod/Node.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
/**
44
* the data structure to create list.
55
*/
6-
public class Node {
7-
int[] nums;
8-
Node next;
6+
public class Node { //NOPMD
7+
/**
8+
* the list of numbers.
9+
*/
10+
public int[] nums;
11+
/**
12+
* the pointer of the next node.
13+
*/
14+
public Node next;
915
}

compose-method/src/test/java/com/iluwatar/composemethod/AppTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44

55
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
66

7-
public class AppTest {
7+
/**
8+
* Application test
9+
*/
10+
class AppTest {//NOPMD
11+
812
@Test
9-
void shouldExecuteWithoutException() {
13+
/**
14+
* no exception to execute.
15+
*/
16+
public void shouldExecuteWithoutException() {
1017
assertDoesNotThrow(() -> App.main(new String[]{}));
1118
}
1219
}

compose-method/src/test/java/com/iluwatar/composemethod/MethodEqualTest.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66

77
import static org.junit.jupiter.api.Assertions.assertEquals;
88

9-
public class MethodEqualTest {
9+
/**
10+
* to test whether the two methods are equal.
11+
*/
12+
class MethodEqualTest {//NOPMD
13+
1014
@Test
11-
void methodsEqual() {
12-
Node head = new Node();
13-
Random random = new Random();
14-
int nodes = random.nextInt(10);
15+
/**
16+
* test two methods are equal.
17+
*/
18+
public void methodsEqual() {
19+
final Node head = new Node();
20+
final Random random = new Random();
21+
final int nodes = random.nextInt(10);
1522
Node tail = head;
1623

1724
//for each node to initialize the integer list.
@@ -20,13 +27,13 @@ void methodsEqual() {
2027
head.nums[j] = random.nextInt(20) - 10;
2128
}
2229
for (int i = 0; i < nodes; i++) {
23-
tail.next = new Node();
30+
tail.next = new Node();//NOPMD
2431
tail = tail.next;
25-
tail.nums = new int[random.nextInt(10)];
32+
tail.nums = new int[random.nextInt(10)];//NOPMD
2633
for (int j = 0; j < tail.nums.length; j++) {
2734
tail.nums[j] = random.nextInt(20) - 10;
2835
}
2936
}
30-
assertEquals(AddImpl.addPositiveNodesRefined(tail), AddImpl.addPositiveNodesOriginal(tail));
37+
assertEquals(AddImpl.addPositiveNodesRefined(tail), AddImpl.addPositiveNodesOriginal(tail), "Two results are equals.");
3138
}
3239
}

0 commit comments

Comments
 (0)