Lazy Iniitialization

Lazy Initialization

The following descriptions of lazy initialization and lazy factory are taken from Wikipedia:

In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. It is a kind of lazy evaluation that refers specifically to the instantiation of objects or other resources. This is typically accomplished by augmenting an accessor method (or property getter) to check whether a private member, acting as a cache, has already been initialized. If it has, it is returned straight away. If not, a new instance is created, placed into the member variable, and returned to the caller just-in-time for its first use. If objects have properties that are rarely used, this can improve startup speed. Mean average program performance may be slightly worse in terms of memory (for the condition variables) and execution cycles (to check them), but the impact of object instantiation is spread in time ("amortized") rather than concentrated in the startup phase of a system, and thus median response times can be greatly improved.

In multithreaded code, access to lazy-initialized objects/state must be synchronized to guard against race conditions.

The Lazy Factory

In a software design pattern view, lazy initialization is often used together with a factory method pattern. This combines three ideas:

  • Using a factory method to create instances of a class (factory method pattern)
  • Storing the instances in a map, and returning the same instance to each request for an instance with same parameters (multiton pattern)
  • Using lazy initialization to instantiate the object the first time it is requested (lazy initialization pattern)