Limiting expensive initialization using the caching pattern
The caching pattern is not found in the traditional list from the Gang of Four (GoF). However, due to industry requirements and resource usage, it has been identified as a commonly used approach and has gained importance.
Motivation
The caching pattern supports element reuse. It does not create a new element on demand – instead, it reuses an already-created element stored in the cache. It stores frequently needed data in fast-access storage for increased performance. Reading data from the cache is faster than instantiating a new entity given the low complexity of fetching the required element.
Finding it in the JDK
The java.base
module and its java.lang
package provide wrapper classes for primitive types. The valueOf
method for double, float, integer, byte, or character types uses a caching mechanism for frequently requested values to reduce memory space and improve performance.
Sample code
Let us...