You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enable easy conversion of Java objects to and from a serialized format, allowing them to be easily stored and transferred.
13
18
14
19
## Explanation
15
-
Java serialization allow us to convert the object to a set of bytes. We can store these bytes into database as
16
-
BLOB(binary long objects) and read them at any time and reconstruct them into Java objects.
17
20
21
+
Real-world example
22
+
23
+
> An example of the Serialized Entity design pattern in the real world can be found in the process of saving and loading game state in video games. When a player decides to save their progress, the current state of the game, including the player's position, inventory, and achievements, is serialized into a file format such as JSON or binary. This file can then be stored on the player's device or on a cloud server. When the player wants to resume their game, the serialized data is deserialized back into the game's objects, allowing the player to continue from where they left off. This mechanism ensures that complex game states can be easily saved and restored, providing a seamless gaming experience.
24
+
25
+
In plain words
26
+
27
+
> The Serialized Entity design pattern enables the conversion of Java objects to and from a serialized format for easy storage and transfer.
28
+
29
+
Wikipedia says
30
+
31
+
> In computing, serialization is the process of translating a data structure or object state into a format that can be stored (e.g. files in secondary storage devices, data buffers in primary storage devices) or transmitted (e.g. data streams over computer networks) and reconstructed later (possibly in a different computer environment). When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward. Serialization of objects does not include any of their associated methods with which they were previously linked.
18
32
19
33
**Programmatic Example**
20
34
21
-
Walking through our customers example, here's the basic `Customer` entity.
35
+
The Serialized Entity design pattern is a way to easily persist Java objects to the database. It uses the Serializable interface and the DAO (Data Access Object) pattern. The pattern first uses Serializable to convert a Java object into a set of bytes, then it uses the DAO pattern to store this set of bytes as a BLOB (Binary Large OBject) in the database.
36
+
37
+
First, we have the `Country` class, which is a simple POJO (Plain Old Java Object) that represents the data that will be serialized and stored in the database. It implements the `Serializable` interface, which means it can be converted to a byte stream and restored from it.
22
38
23
39
```java
40
+
24
41
@Getter
25
42
@Setter
26
43
@EqualsAndHashCode
@@ -32,190 +49,155 @@ public class Country implements Serializable {
32
49
privateString name;
33
50
privateString continents;
34
51
privateString language;
35
-
publicstaticfinallong serialVersionUID =7149851;
36
-
// Constructor ->
37
-
// getters and setters ->
38
-
}
52
+
@Serial
53
+
privatestaticfinallong serialVersionUID =7149851;
39
54
55
+
}
40
56
```
41
-
Here is `CountrySchemaSql`, this class have method allow us to serialize `Country` object and insert it into the
42
-
database, also have a method that read serialized data from the database and deserialize it to `Country`object.
57
+
58
+
Next, we have the `CountryDao` interface, which defines the methods for persisting and retrieving `Country`objects from the database.
43
59
44
60
```java
45
-
@Slf4j
46
-
publicclassCountrySchemaSql {
47
-
publicstaticfinalStringCREATE_SCHEMA_SQL="CREATE TABLE IF NOT EXISTS WORLD (ID INT PRIMARY KEY, COUNTRY BLOB)";
48
-
49
-
publicstaticfinalStringDELETE_SCHEMA_SQL="DROP TABLE WORLD IF EXISTS";
This `App.java` will first delete a World table from the h2 database(if there is one) and create a new table called
129
-
`WORLD` to ensure we have a table we want.
130
-
It will then create two `Country` objects called `China` and `UnitedArabEmirates`, then create two `CountrySchemaSql`
131
-
objects and use objects `China` and `UnitedArabEmirates` as arguments.
132
-
Finally, call `SerializedChina.insertCountry()` and `serializedUnitedArabEmirates.insertCountry()` to serialize and
133
-
store them to database, and call `SerializedChina.selectCountry()` and `serializedUnitedArabEmirates.selectCountry()`
134
-
methods to read and deserialize data items to `Country` objects.
68
+
The `CountrySchemaSql` class implements the `CountryDao` interface. It uses a `DataSource` to connect to the database and a `Country` object that it will serialize and store in the database.
Finally, in the `App` class, we create `Country` objects and `CountrySchemaSql` objects. We then call the `insertCountry` method to serialize the `Country` objects and store them in the database. We also call the `selectCountry` method to retrieve the serialized `Country` objects from the database and deserialize them back into `Country` objects.
This is a basic example of the Serialized Entity design pattern. It shows how to serialize Java objects, store them in a database, and then retrieve and deserialize them.
*[Data Transfer Object (DTO)](https://java-design-patterns.com/patterns/data-transfer-object/): Used to encapsulate data and send it over the network. Often serialized for transmission.
195
+
*[Proxy](https://java-design-patterns.com/patterns/proxy/): Proxies can serialize requests to interact with remote objects.
196
+
*[Memento](https://java-design-patterns.com/patterns/memento/): Provides a way to capture and restore an object's state, often using serialization.
217
197
218
198
## Credits
219
199
220
-
*[J2EE Design Patterns by William Crawford, Jonathan Kaplan](https://www.oreilly.com/library/view/j2ee-design-patterns/0596004273/re21.html)
221
-
*[komminen](https://github.com/komminen/java-design-patterns) (His attempts of serialized-entity inspired me and learned a lot from his code)
200
+
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
201
+
*[Effective Java](https://amzn.to/4cGk2Jz)
202
+
*[J2EE Design Patterns](https://amzn.to/4dpzgmx)
203
+
*[Java Concurrency in Practice](https://amzn.to/4aRMruW)
0 commit comments