Lazy initialization
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 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 and execution cycles, but the impact of object instantiation is spread in time 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
- Storing the instances in a map, and returning the same instance to each request for an instance with same parameters
- Using lazy initialization to instantiate the object the first time it is requested
Examples
ActionScript 3
The following is an example of a class with lazy initialization implemented in ActionScript:package examples.lazyinstantiation
Basic Usage:
package
C
In C, lazy evaluation would normally be implemented inside a single function, or a single source file, using static variables.In a function:
- include
- include
- include
- include
struct fruit *get_fruit
/* Example code */
int main
Using a single source file instead allows the state to be shared between multiple functions, while still hiding it from non-related functions.
fruit.h:
- ifndef _FRUIT_INCLUDED_
- define _FRUIT_INCLUDED_
struct fruit *get_fruit;
void print_fruit_list;
- endif /* _FRUIT_INCLUDED_ */
fruit.c:
- include
- include
- include
- include
- include "fruit.h"
static int seq;
struct fruit *get_fruit
void print_fruit_list
main.c:
- include
- include
- include "fruit.h"
C#
In.NET Framework 4.0 Microsoft has included aLazy
class that can be used to do lazy loading.Below is some dummy code that does lazy loading of Class
Fruit
var lazyFruit = new Lazy
Fruit fruit = lazyFruit.Value;
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
.using System;
using System.Collections;
using System.Collections.Generic;
public class Fruit
class Program
A fairly straightforward 'fill-in-the-blanks' example of a Lazy Initialization design pattern, except that this uses an enumeration for the type
namespace DesignPatterns.LazyInitialization
C++
Here is an example in C++.- include
- include
- include
// static
std::map
// Lazy Factory method, gets the |Fruit| instance associated with a certain
// |type|. Creates new ones as needed.
Fruit* Fruit::GetFruit
// For example purposes to see pattern in action.
void Fruit::PrintCurrentTypes
int main
// OUTPUT:
//
// Number of instances made = 1
// Banana
//
// Number of instances made = 2
// Apple
// Banana
//
// Number of instances made = 2
// Apple
// Banana
//
Crystal
class Fruit
private getter type : String
@@types = of String => Fruit
def initialize
end
def self.get_fruit_by_type
@@types ||= Fruit.new
end
def self.show_all
puts "Number of instances made: #"
@@types.each do |type, fruit|
puts "#"
end
puts
end
def self.size
@@types.size
end
end
Fruit.get_fruit_by_type
Fruit.show_all
Fruit.get_fruit_by_type
Fruit.show_all
Fruit.get_fruit_by_type
Fruit.show_all
Output:
Number of instances made: 1
Banana
Number of instances made: 2
Banana
Apple
Number of instances made: 2
Banana
Apple
Haxe
Here is an example in Haxeclass Fruit
class Test
Java
Here is an example in Java.import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class Program
enum FruitType
class Fruit
Output
Number of instances made = 1
Banana
Number of instances made = 2
Banana
Apple
Number of instances made = 2
Banana
Apple
JavaScript
Here is an example in JavaScript.var Fruit = ;
Fruit.getFruit;
Fruit.printCurrentTypes;
Fruit.getFruit;
Fruit.printCurrentTypes;
Fruit.getFruit;
Fruit.printCurrentTypes;
Output
Number of instances made: 1
Apple
Number of instances made: 2
Apple
Banana
Number of instances made: 2
Apple
Banana
PHP
Here is an example of lazy initialization in PHP 7.4:header;
class Fruit
Fruit::getFruit;
Fruit::printCurrentTypes;
Fruit::getFruit;
Fruit::printCurrentTypes;
Fruit::getFruit;
Fruit::printCurrentTypes;
/*
OUTPUT:
Number of instances made: 1
Apple
Number of instances made: 2
Apple
Banana
Number of instances made: 2
Apple
Banana
- /
Python
Here is an example in Python.class Fruit:
def __init__ -> None:
self.item = item
class Fruits:
def __init__ -> None:
self.items =
def get_fruit -> Fruit:
if item not in self.items:
self.items = Fruit
return self.items
if __name__ '__main__':
fruits = Fruits
Ruby
Here is an example 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.require 'net/http'
class Blogger
def auth_token
@auth_token ||=
&&
get_token_from_http_response
end
# get_token_from_http_response, uri and params are defined later in the class
end
b = Blogger.new
b.instance_variable_get # returns nil
b.auth_token # returns token
b.instance_variable_get # returns token
Scala
has built-in support for lazy variable initiation.scala> val x =
Hello
x: Int = 99
scala> lazy val y =
y: Int =
scala> y
Hello!!
res2: Int = 31
scala> y
res3: Int = 31
Smalltalk
Here is an example in Smalltalk, of a typical accessor method to return the value of a variable using lazy initialization.height
^height ifNil: .
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.
initialize
height := 2.0
height
^height
Note that lazy initialization can also be used in non-object-oriented languages.