Skip to content

Commit 29edeab

Browse files
Evan Sia Wai SuanEvan Sia Wai Suan
authored andcommitted
Fixes based on code review feedback
1 parent abcc398 commit 29edeab

File tree

7 files changed

+60
-47
lines changed

7 files changed

+60
-47
lines changed

dirty-flag/etc/dirty-flag.png

-1.64 KB
Loading

dirty-flag/etc/dirty-flag.ucls

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.2.2" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3+
realizations="true" associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="com.iluwatar.dirtyflag.App" project="dirty-flag"
5+
file="/dirty-flag/src/main/java/com/iluwatar/dirtyflag/App.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="266" y="188"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</class>
13+
<class id="2" language="java" name="com.iluwatar.dirtyflag.DataFetcher" project="dirty-flag"
14+
file="/dirty-flag/src/main/java/com/iluwatar/dirtyflag/DataFetcher.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="153" width="125" x="66" y="291"/>
16+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</class>
22+
<class id="3" language="java" name="com.iluwatar.dirtyflag.World" project="dirty-flag"
23+
file="/dirty-flag/src/main/java/com/iluwatar/dirtyflag/World.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="-1" width="-1" x="379" y="366"/>
25+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
26+
sort-features="false" accessors="true" visibility="true">
27+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
28+
<operations public="true" package="true" protected="true" private="true" static="true"/>
29+
</display>
30+
</class>
31+
<association id="4">
32+
<end type="SOURCE" refId="3" navigable="false">
33+
<attribute id="5" name="df"/>
34+
<multiplicity id="6" minimum="0" maximum="1"/>
35+
</end>
36+
<end type="TARGET" refId="2" navigable="true"/>
37+
<display labels="true" multiplicity="true"/>
38+
</association>
39+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
40+
sort-features="false" accessors="true" visibility="true">
41+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
42+
<operations public="true" package="true" protected="true" private="true" static="true"/>
43+
</classifier-display>
44+
<association-display labels="true" multiplicity="true"/>
45+
</class-diagram>

dirty-flag/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
<parent>
66
<groupId>com.iluwatar</groupId>
77
<artifactId>java-design-patterns</artifactId>
8-
<version>1.19.0-SNAPSHOT</version>
8+
<version>1.20.0-SNAPSHOT</version>
99
</parent>
1010
<groupId>com.iluwatar</groupId>
1111
<artifactId>dirty-flag</artifactId>
12-
<version>1.19.0-SNAPSHOT</version>
12+
<version>1.20.0-SNAPSHOT</version>
1313
<name>dirty-flag</name>
1414
<url>http://maven.apache.org</url>
1515
<properties>

dirty-flag/src/main/java/com/iluwatar/dirtyflag/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void run() {
5757
executorService.scheduleAtFixedRate(new Runnable() {
5858
@Override
5959
public void run() {
60-
World world = World.getInstance();
60+
World world = new World();
6161
List<String> countries = world.fetch();
6262
System.out.println("Our world currently has the following countries:-");
6363
for (String country : countries) {

dirty-flag/src/main/java/com/iluwatar/dirtyflag/DataFetcher.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,11 @@
1515
*/
1616
public class DataFetcher {
1717

18-
private static DataFetcher df;
1918
private final String filename = "world.txt";
20-
private long lastFetched = -1;
19+
private long lastFetched;
2120

22-
private DataFetcher() {
23-
}
24-
25-
/**
26-
* Init.
27-
*
28-
* @return DataFetcher instance
29-
*/
30-
public static DataFetcher getInstance() {
31-
if (df == null) {
32-
df = new DataFetcher();
33-
}
34-
return df;
21+
public DataFetcher() {
22+
this.lastFetched = -1;
3523
}
3624

3725
private boolean isDirty(long fileLastModified) {
@@ -66,6 +54,6 @@ public List<String> fetch() {
6654
return data;
6755
}
6856

69-
return null;
57+
return new ArrayList<String>();
7058
}
7159
}

dirty-flag/src/main/java/com/iluwatar/dirtyflag/World.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,12 @@
1212
*/
1313
public class World {
1414

15-
private static World world;
16-
private static List<String> countries = new ArrayList<String>();
15+
private List<String> countries;
16+
private DataFetcher df;
1717

18-
private World() {
19-
}
20-
21-
/**
22-
* Init.
23-
*
24-
* @return World instance
25-
*/
26-
public static World getInstance() {
27-
if (world == null) {
28-
world = new World();
29-
}
30-
return world;
18+
public World() {
19+
this.countries = new ArrayList<String>();
20+
this.df = new DataFetcher();
3121
}
3222

3323
/**
@@ -37,10 +27,9 @@ public static World getInstance() {
3727
* @return List of strings
3828
*/
3929
public List<String> fetch() {
40-
DataFetcher df = DataFetcher.getInstance();
4130
List<String> data = df.fetch();
4231

43-
countries = data == null ? countries : data;
32+
countries = data.isEmpty() ? countries : data;
4433

4534
return countries;
4635
}

dirty-flag/src/test/java/org/dirty/flag/DirtyFlagTest.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424

2525
import static org.junit.jupiter.api.Assertions.assertTrue;
2626

27-
import java.lang.reflect.Field;
2827
import java.util.List;
2928

30-
import org.junit.jupiter.api.BeforeEach;
3129
import org.junit.jupiter.api.Test;
3230

3331
import com.iluwatar.dirtyflag.DataFetcher;
@@ -39,23 +37,16 @@
3937
*/
4038
public class DirtyFlagTest {
4139

42-
@BeforeEach
43-
public void reset() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
44-
Field instance = DataFetcher.class.getDeclaredField("df");
45-
instance.setAccessible(true);
46-
instance.set(null, null);
47-
}
48-
4940
@Test
5041
public void testIsDirty() {
51-
DataFetcher df = DataFetcher.getInstance();
42+
DataFetcher df = new DataFetcher();
5243
List<String> countries = df.fetch();
5344
assertTrue(!countries.isEmpty());
5445
}
5546

5647
@Test
5748
public void testIsNotDirty() {
58-
DataFetcher df = DataFetcher.getInstance();
49+
DataFetcher df = new DataFetcher();
5950
df.fetch();
6051
List<String> countries = df.fetch();
6152
assertTrue(countries == null);

0 commit comments

Comments
 (0)