53 - Java Serialization with Singleton pattern and readResolve() - Unit Tests Code Demo
Автор: Rishi’s programming channel
Загружено: 2023-01-03
Просмотров: 148
Описание:
@backstreetbrogrammer
--------------------------------------------------------------------------------
Chapter 20 - Java Serialization with Singleton pattern and readResolve() - Unit Tests Code Demo
--------------------------------------------------------------------------------
In software engineering, the Singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance.
The pattern is useful when exactly one object is needed to coordinate actions across a system.
More specifically, the singleton pattern allows objects to:
Ensure they only have one instance
Provide easy access to that instance
Control their instantiation by hiding the constructors of a class
As we know that deserialization process will always contain the “copy” of the original object =: thus it will break the singleton design pattern as only ONE and SAME instance has to be there in a current JVM run.
In other words, any class would no longer be a singleton if it implements Serializable interface.
It doesn’t matter whether the class uses the default serialized form or a custom serialized form, nor does it matter whether the class provides an explicit readObject() method.
Any readObject() method, whether explicit or default, returns a newly created instance, which will not be the same instance that was created at class initialization time.
To solve this issue, the readResolve() method allows to substitute another instance for the one created by readObject().
If the class of an object being deserialized defines a readResolve() method with the proper declaration, this method is invoked on the newly created object after it is deserialized.
The object reference returned by this method is then returned in place of the newly created object. No reference to the newly created object is retained, so it immediately becomes eligible for garbage collection.
Java Serialization Playlist: • Java Serialization
Dynamic Programming Playlist: • Dynamic Programming
#java #javadevelopers #javaprogramming #javaserialization
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: