Serialize a Singleton in Java (#13074)

* Added a new module. Also added Serializable Singleton article source code.

* Modified the unit tests. And deleted the main classes.
This commit is contained in:
Arya 2022-11-25 21:03:26 +03:30 committed by GitHub
parent 8ee7dcd350
commit b7f097bb92
6 changed files with 203 additions and 0 deletions

View File

@ -0,0 +1 @@
### Relevant Articles:

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>design-patterns-creational-2</artifactId>
<version>1.0</version>
<name>design-patterns-creational-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>patterns-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,20 @@
package com.baeldung.serializable_singleton;
public enum EnumSingleton {
INSTANCE("State Zero");
private String state;
private EnumSingleton(String state) {
this.state = state;
}
public static EnumSingleton getInstance() {
return INSTANCE;
}
public String getState() { return this.state; }
public void setState(String state) { this.state = state; }
}

View File

@ -0,0 +1,28 @@
package com.baeldung.serializable_singleton;
import java.io.Serializable;
public class Singleton implements Serializable {
private static Singleton INSTANCE;
private String state = "State Zero";
private Singleton() {
}
public static Singleton getInstance() {
if(INSTANCE == null) {
INSTANCE = new Singleton();
}
return INSTANCE;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}

View File

@ -0,0 +1,69 @@
package com.baeldung.serializable_singleton;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
// Unit test for the EnumSingleton class.
public class EnumSingletonUnitTest {
// Checks that when an EnumSingleton instance is serialized
// and then deserialized, its state is preserved.
@Test
public void givenEnumSingleton_whenSerializedAndDeserialized_thenStatePreserved() {
EnumSingleton es1 = EnumSingleton.getInstance();
es1.setState("State One");
try (
FileOutputStream fos = new FileOutputStream("enum_singleton_test.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream("enum_singleton_test.txt");
ObjectInputStream ois = new ObjectInputStream(fis)) {
// Serializing.
oos.writeObject(es1);
// Deserializing.
EnumSingleton es2 = (EnumSingleton) ois.readObject();
// Checking if the state is preserved.
assertEquals(es1.getState(), es2.getState());
} catch (Exception e) {
System.out.println(e);
}
}
// Checking that when an EnumSingleton instance is serialized
// and then deserialized, then there is still one instance
// of the EnumSingleton class in memory.
@Test
public void givenEnumSingleton_whenSerializedAndDeserialized_thenOneInstance() {
EnumSingleton es1 = EnumSingleton.getInstance();
try (
FileOutputStream fos = new FileOutputStream("enum_singleton_test.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream("enum_singleton_test.txt");
ObjectInputStream ois = new ObjectInputStream(fis)) {
// Serializing.
oos.writeObject(es1);
// Deserializing.
EnumSingleton es2 = (EnumSingleton) ois.readObject();
// Checking if es1 and es2 are pointing to
// the same instance in memory.
assertEquals(es1, es2);
} catch (Exception e) {
System.out.println(e);
}
}
}

View File

@ -0,0 +1,68 @@
package com.baeldung.serializable_singleton;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
// Unit test for the Singleton class.
public class SingletonUnitTest {
// Checks that when a Singleton instance is serialized
// and then deserialized, its state is preserved.
@Test
public void givenSingleton_whenSerializedAndDeserialized_thenStatePreserved() {
Singleton s1 = Singleton.getInstance();
s1.setState("State One");
try (
FileOutputStream fos = new FileOutputStream("singleton_test.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream("singleton_test.txt");
ObjectInputStream ois = new ObjectInputStream(fis)) {
// Serializing.
oos.writeObject(s1);
// Deserializing.
Singleton s2 = (Singleton) ois.readObject();
// Checking if the state is preserved.
assertEquals(s1.getState(), s2.getState());
} catch (Exception e) {
System.out.println(e);
}
}
// Checking that when a Singleton instance is serialized
// and then deserialized, then there are two instances of
// the Singleton class.
@Test
public void givenSingleton_whenSerializedAndDeserialized_thenTwoInstances() {
Singleton s1 = Singleton.getInstance();
try (
FileOutputStream fos = new FileOutputStream("singleton_test.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream("singleton_test.txt");
ObjectInputStream ois = new ObjectInputStream(fis)) {
// Serializing.
oos.writeObject(s1);
// Deserializing.
Singleton s2 = (Singleton) ois.readObject();
// Checking if s1 and s2 are not the same instance.
assertNotEquals(s1, s2);
} catch (Exception e) {
System.out.println(e);
}
}
}