Skip to content

Commit 9cc5cfc

Browse files
committed
Code refactor
1 parent 1432850 commit 9cc5cfc

File tree

12 files changed

+223
-25
lines changed

12 files changed

+223
-25
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
clean-code.iml
3+
target

journal.text

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Learn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn Guitar
1+
Learn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn GuitarLearn to CodeLearn to CodeLearn Guitar

singleton.ser

-3 Bytes
Binary file not shown.

src/main/java/com/javamultiplex/pattern/creational/singleton/BasicSingleton.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package com.javamultiplex.pattern.creational.singleton;
22

3-
import java.io.Serializable;
4-
53
/**
64
* @author Rohit Agarwal on 27/08/20 11:59 pm
75
* @copyright www.javamultiplex.com
86
*/
9-
public class BasicSingleton implements Serializable {
10-
11-
private static final long serialVersionUID = 1417853001742788063L;
7+
public class BasicSingleton {
128
private static final BasicSingleton INSTANCE = new BasicSingleton();
139
private int value;
1410

@@ -26,8 +22,4 @@ public int getValue() {
2622
public void setValue(int value) {
2723
this.value = value;
2824
}
29-
30-
public Object readResolve() {
31-
return INSTANCE;
32-
}
3325
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.javamultiplex.pattern.creational.singleton;
2+
3+
/**
4+
* @author Rohit Agarwal on 27/08/20 11:59 pm
5+
* @copyright www.javamultiplex.com
6+
*/
7+
public class SingletonV2 {
8+
private static final SingletonV2 INSTANCE = new SingletonV2();
9+
private int value;
10+
11+
private SingletonV2() {
12+
if (INSTANCE != null) {
13+
throw new InstantiationError("Object Creation is not allowed.");
14+
}
15+
}
16+
17+
public static SingletonV2 getInstance() {
18+
return INSTANCE;
19+
}
20+
21+
public int getValue() {
22+
return value;
23+
}
24+
25+
public void setValue(int value) {
26+
this.value = value;
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.javamultiplex.pattern.creational.singleton;
2+
3+
/**
4+
* @author Rohit Agarwal on 27/08/20 11:59 pm
5+
* @copyright www.javamultiplex.com
6+
*/
7+
public class SingletonV3 implements Cloneable {
8+
private static final SingletonV3 INSTANCE = new SingletonV3();
9+
private int value;
10+
11+
private SingletonV3() {
12+
if (INSTANCE != null) {
13+
throw new InstantiationError("Object Creation is not allowed.");
14+
}
15+
}
16+
17+
public static SingletonV3 getInstance() {
18+
return INSTANCE;
19+
}
20+
21+
public int getValue() {
22+
return value;
23+
}
24+
25+
public void setValue(int value) {
26+
this.value = value;
27+
}
28+
29+
@Override
30+
protected Object clone() throws CloneNotSupportedException {
31+
throw new CloneNotSupportedException("Object cloning not supported.");
32+
}
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.javamultiplex.pattern.creational.singleton;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* @author Rohit Agarwal on 27/08/20 11:59 pm
7+
* @copyright www.javamultiplex.com
8+
*/
9+
public class SingletonV4 implements Cloneable, Serializable {
10+
private static final long serialVersionUID = -6942288218575793523L;
11+
private static final SingletonV4 INSTANCE = new SingletonV4();
12+
private int value;
13+
14+
private SingletonV4() {
15+
if (INSTANCE != null) {
16+
throw new InstantiationError("Object Creation is not allowed.");
17+
}
18+
}
19+
20+
public static SingletonV4 getInstance() {
21+
return INSTANCE;
22+
}
23+
24+
public int getValue() {
25+
return value;
26+
}
27+
28+
public void setValue(int value) {
29+
this.value = value;
30+
}
31+
32+
@Override
33+
protected Object clone() throws CloneNotSupportedException {
34+
throw new CloneNotSupportedException("Object cloning not supported.");
35+
}
36+
37+
public Object readResolve() {
38+
return INSTANCE;
39+
}
40+
}

src/test/java/com/javamultiplex/pattern/creational/singleton/BasicSingletonClient.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.javamultiplex.pattern.creational.singleton;
22

3-
import com.javamultiplex.util.SerializationUtil;
43
import org.junit.jupiter.api.Test;
54

6-
import java.io.IOException;
75
import java.lang.reflect.Constructor;
86
import java.lang.reflect.InvocationTargetException;
97
import java.util.Arrays;
@@ -18,6 +16,16 @@
1816
*/
1917
public class BasicSingletonClient {
2018

19+
20+
@Test
21+
public void singletonTest() {
22+
BasicSingleton instance1 = BasicSingleton.getInstance();
23+
BasicSingleton instance2 = BasicSingleton.getInstance();
24+
System.out.println(instance1.hashCode());
25+
System.out.println(instance2.hashCode());
26+
assertSame(instance1, instance2);
27+
}
28+
2129
@Test
2230
public void shouldCreateTwoInstancesUsingReflection() throws IllegalAccessException, InvocationTargetException, InstantiationException {
2331
BasicSingleton basicSingleton = BasicSingleton.getInstance();
@@ -43,16 +51,4 @@ public void shouldCreateTwoInstancesUsingReflection() throws IllegalAccessExcept
4351
assertEquals(110, basicSingleton.getValue());
4452
assertEquals(30, newInstance.getValue());
4553
}
46-
47-
@Test
48-
public void shouldNotCreateTwoInstancesUsingSerialization() throws IOException, ClassNotFoundException {
49-
BasicSingleton basicSingleton = BasicSingleton.getInstance();
50-
basicSingleton.setValue(110);
51-
SerializationUtil.serialize(basicSingleton, "singleton.ser");
52-
BasicSingleton another = (BasicSingleton) SerializationUtil.deserialize("singleton.ser");
53-
another.setValue(11);
54-
assertSame(basicSingleton, another);
55-
assertEquals(11, basicSingleton.getValue());
56-
assertEquals(11, another.getValue());
57-
}
5854
}

src/test/java/com/javamultiplex/pattern/creational/singleton/EnumBasedSingletonClient.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import java.io.IOException;
77

8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertSame;
10+
811
/**
912
* @author Rohit Agarwal on 29/08/20 11:21 pm
1013
* @copyright www.javamultiplex.com
@@ -18,6 +21,11 @@ public void shouldNotCreateTwoInstances() throws IOException, ClassNotFoundExcep
1821
System.out.println(enumBasedSingleton.name());
1922
SerializationUtil.serialize(enumBasedSingleton, "enum-singleton.ser");
2023
EnumBasedSingleton another = (EnumBasedSingleton) SerializationUtil.deserialize("enum-singleton.ser");
21-
System.out.println(another.getValue());
24+
assertEquals(13, another.getValue());
25+
assertEquals(13, enumBasedSingleton.getValue());
26+
another.setValue(100);
27+
assertSame(enumBasedSingleton, another);
28+
assertEquals(100, another.getValue());
29+
assertEquals(100, enumBasedSingleton.getValue());
2230
}
2331
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.javamultiplex.pattern.creational.singleton;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.lang.reflect.Constructor;
7+
import java.lang.reflect.InvocationTargetException;
8+
import java.util.Arrays;
9+
import java.util.NoSuchElementException;
10+
import java.util.Optional;
11+
12+
/**
13+
* @author Rohit Agarwal on 28/08/20 12:04 am
14+
* @copyright www.javamultiplex.com
15+
*/
16+
public class SingletonV2Client {
17+
18+
@Test
19+
public void shouldNotCreateTwoInstancesUsingReflection() throws IllegalAccessException, InvocationTargetException, InstantiationException {
20+
SingletonV2 singletonV2 = SingletonV2.getInstance();
21+
singletonV2.setValue(110);
22+
Class<SingletonV2> singletonV2Class = SingletonV2.class;
23+
Constructor<?>[] constructors = singletonV2Class.getDeclaredConstructors();
24+
Optional<Constructor<?>> privateConstructor = Arrays.stream(constructors)
25+
.filter(constructor -> !constructor.isAccessible())
26+
.findAny();
27+
Constructor<?> constructor;
28+
if (privateConstructor.isPresent()) {
29+
constructor = privateConstructor.get();
30+
constructor.setAccessible(true);
31+
} else {
32+
throw new NoSuchElementException("No private constructor found");
33+
}
34+
InvocationTargetException invocationTargetException = Assertions.assertThrows(InvocationTargetException.class, () ->
35+
{
36+
SingletonV2 newInstance = (SingletonV2) constructor.newInstance();
37+
newInstance.setValue(30);
38+
});
39+
Assertions.assertEquals(InstantiationError.class, invocationTargetException.getCause().getClass());
40+
Assertions.assertEquals("Object Creation is not allowed.", invocationTargetException.getCause().getMessage());
41+
}
42+
}

0 commit comments

Comments
 (0)