In a software design pattern view, lazy initialization is often used together with a factory method pattern. This combines three ideas:
The following is an example of a class with lazy initialization implemented in ActionScript:
Basic use:
In C, lazy evaluation would normally be implemented inside one function, or one source file, using static variables.
In a function:
Using one source file instead allows the state to be shared between multiple functions, while still hiding it from non-related functions.
fruit.h:
fruit.c:
main.c:
In .NET Framework 4.0 Microsoft has included a Lazy class that can be used to do lazy loading. Below is some dummy code that does lazy loading of Class Fruit
Here is a dummy example in C#.
The Fruit class itself doesn't do anything here, The class variable _typesDictionary is a Dictionary/Map used to store Fruit instances by typeName.
A fairly straightforward 'fill-in-the-blanks' example of a Lazy Initialization design pattern, except that this uses an enumeration for the type
This example is in C++.
Output:
This example is in Haxe.1
Usage
This example is in Java.
Output
This example is in JavaScript.
Here is an example of lazy initialization in PHP 7.4:
This example is in Python.
This example is in Ruby, of lazily initializing an authentication token from a remote service like Google. The way that @auth_token is cached is also an example of memoization.
Rust have std::cell::LazyCell.2
Scala has built-in support for lazy variable initiation.3
This example is in Smalltalk, of a typical accessor method to return the value of a variable using lazy initialization.
The 'non-lazy' alternative is to use an initialization method that is run when the object is created and then use a simpler accessor method to fetch the value.
Note that lazy initialization can also be used in non-object-oriented languages.
In the field of theoretical computer science, lazy initialization4 (also called a lazy array) is a technique to design data structures that can work with memory that does not need to be initialized. Specifically, assume that we have access to a table T of n uninitialized memory cells (numbered from 1 to n), and want to assign m cells of this array, e.g., we want to assign T[ki] := vi for pairs (k1, v1), ..., (km, vm) with all ki being different. The lazy initialization technique allows us to do this in just O(m) operations, rather than spending O(m+n) operations to first initialize all array cells. The technique is simply to allocate a table V storing the pairs (ki, vi) in some arbitrary order, and to write for each i in the cell T[ki] the position in V where key ki is stored, leaving the other cells of T uninitialized. This can be used to handle queries in the following fashion: when we look up cell T[k] for some k, we can check if T[k] is in the range {1, ..., m}: if it is not, then T[k] is uninitialized. Otherwise, we check V[T[k]], and verify that the first component of this pair is equal to k. If it is not, then T[k] is uninitialized (and just happened by accident to fall in the range {1, ..., m}). Otherwise, we know that T[k] is indeed one of the initialized cells, and the corresponding value is the second component of the pair.
"Lazy initialization - Design patterns - Haxe programming language cookbook". 2018-01-11. Retrieved 2018-11-09. https://code.haxe.org/category/design-patterns/lazy-initialization.html ↩
"LazyCell in std::cell - Rust". doc.rust-lang.org. Retrieved 18 January 2025. https://doc.rust-lang.org/std/cell/struct.LazyCell.html ↩
Pollak, David (2009-05-25). Beginning Scala. Apress. ISBN 9781430219897. 9781430219897 ↩
Moret, B. M. E.; Shapiro, H. D. (1991). Algorithms from P to NP, Volume 1: Design & Efficiency. Benjamin/Cummings Publishing Company. pp. 191–192. ISBN 0-8053-8008-6. 0-8053-8008-6 ↩