Skip to content

Commit 0a2a54b

Browse files
authored
Add singleton examples with tests (#36)
1 parent 4fefe5b commit 0a2a54b

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package pl.mperor.lab.java.design.pattern.creational.singleton;
2+
3+
public class LazyInitializedSingleton {
4+
5+
private static LazyInitializedSingleton instance;
6+
private final long time = System.currentTimeMillis();
7+
8+
private LazyInitializedSingleton() {
9+
}
10+
11+
public static synchronized LazyInitializedSingleton getInstance() {
12+
if (instance == null) {
13+
instance = new LazyInitializedSingleton();
14+
}
15+
return instance;
16+
}
17+
18+
public long getTime() {
19+
return time;
20+
}
21+
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package pl.mperor.lab.java.design.pattern.creational.singleton;
2+
3+
public class Singleton {
4+
5+
private static final Singleton instance = new Singleton();
6+
private final long time = System.currentTimeMillis();
7+
8+
private Singleton() {
9+
}
10+
11+
public static Singleton getInstance() {
12+
return instance;
13+
}
14+
15+
public long getTime() {
16+
return time;
17+
}
18+
19+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.iterator;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.List;
6+
7+
public class IteratorTest {
8+
9+
private final List<String> names = List.of("Adam", "Bob", "Conor");
10+
11+
@Test
12+
public void testIteratorImperatively() {
13+
findName("Bob");
14+
}
15+
16+
public boolean findName(String n) {
17+
for (int i = 0; i < names.size(); i++) {
18+
if (names.get(i).equals(n)) {
19+
return true;
20+
}
21+
}
22+
23+
// same as foreach
24+
for (String name : names) {
25+
if (name.equals(n)) {
26+
return true;
27+
}
28+
}
29+
30+
return false;
31+
}
32+
33+
@Test
34+
public void testIteratorFunctionally() {
35+
// internal iterator
36+
37+
}
38+
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package pl.mperor.lab.java.design.pattern.creational.singleton;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.concurrent.CompletableFuture;
7+
8+
public class LazyInitializedSingletonTest {
9+
10+
@Test
11+
public void shouldOnlyAllowToLazyCreateOneInstanceOfSingleton() {
12+
var first = CompletableFuture.supplyAsync(() -> {
13+
System.out.println("First ...");
14+
return LazyInitializedSingleton.getInstance();
15+
});
16+
var second = CompletableFuture.supplyAsync(() -> {
17+
System.out.println("Second ...");
18+
return LazyInitializedSingleton.getInstance();
19+
});
20+
21+
LazyInitializedSingleton firstResult = first.join();
22+
LazyInitializedSingleton secondResult = second.join();
23+
24+
Assertions.assertSame(firstResult, secondResult);
25+
Assertions.assertEquals(firstResult.getTime(), secondResult.getTime());
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package pl.mperor.lab.java.design.pattern.creational.singleton;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.concurrent.TimeUnit;
7+
8+
public class SingletonTest {
9+
10+
@Test
11+
public void shouldOnlyAllowToCreateOneInstanceOfSingleton() throws InterruptedException {
12+
var instance = Singleton.getInstance();
13+
long time = instance.getTime();
14+
15+
TimeUnit.MILLISECONDS.sleep(50);
16+
long timeAfterBreak = Singleton.getInstance().getTime();
17+
18+
Assertions.assertSame(instance, Singleton.getInstance());
19+
Assertions.assertEquals(time, timeAfterBreak);
20+
}
21+
22+
}

0 commit comments

Comments
 (0)