Lazy initialization

From Wikipedia, the free encyclopedia

Jump to: navigation, search

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.

This is typically accomplished by maintaining a flag indicating whether the process has taken place. Each time the desired object is summoned, the flag is tested. If it is ready, it is returned. If not, it is initialized on the spot.

Contents

[edit] 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 get instances of a class (factory method pattern)
  • storing the instances in a map, so you get the same instance the next time you ask for an instance with same parameter (compare with a singleton pattern)
  • using lazy initialization to instantiate the object the first time it is requested (lazy initialization pattern).

[edit] Examples

[edit] Java

Here is a dummy example (in Java). The Fruit class itself doesn't do anything here, this is just an example to show the architecture. The class variable types is a map used to store Fruit instances by type.

import java.util.*;
 
public class Fruit
{
    private static final Map<String,Fruit> types = new HashMap<String,Fruit>();
    private final String type;
 
    // using a private constructor to force use of the factory method.
    private Fruit(String type) {
      this.type = type;
    }
 
    /**
     * Lazy Factory method, gets the Fruit instance associated with a
     * certain type. Instantiates new ones as needed.
     * @param type Any string that describes a fruit type, e.g. "apple"
     * @return The Fruit instance associated with that type.
     */
    public static synchronized Fruit getFruit(String type) {
      if(!types.containsKey(type))
      {
        types.put(type, new Fruit(type)); // Lazy initialization
      }
      return types.get(type);
    }
}

[edit] C#

Here is the fruit example in C# (based on a C++ example)

using System;
using System.Collections.Generic;
 
public class Fruit
{
    private static Dictionary<string,Fruit> types = new Dictionary<string,Fruit>();
    private string type;
 
    /// <summary>
    /// using a private constructor to force use of the factory method.
    /// </summary>
    /// <param name="type">Type of fruit</param>
    private Fruit(String type) {
      this.type = type;
    }
 
    /// </summary>
    /// <param name="type">Any string that describes a fruit type, e.g. "apple"</param>
    /// <returns>The Fruit instance associated with that type.</returns>
    public static Fruit getFruit(string type) {
        Fruit f;
 
        if ( ! types.TryGetValue(type, out f) )
        {
          f = new Fruit(type); // lazy initialization
          types.Add(type,f);
        }
 
      return f;
    }
 
    public static void printCurrentTypes() {
        if (types.Count > 0) {
            Console.WriteLine("Number of instances made = {0}",types.Count);
            foreach (KeyValuePair<string,Fruit> kvp in types) 
            {
                Console.WriteLine(kvp.Key);
            }
            Console.WriteLine();
        }
    }
 
}
 
 
 
class Program
{
    static void Main(string[] args)
    {
        Fruit.getFruit("Banana");
        Fruit.printCurrentTypes();
 
        Fruit.getFruit("Apple");
        Fruit.printCurrentTypes();
 
        // returns pre-existing instance from first 
        // time Fruit with "Banana" was created
        Fruit.getFruit("Banana");
        Fruit.printCurrentTypes();
 
        Console.ReadLine();
    }
}

[edit] C++

Here is how you could do the fruit example in C++

#include <iostream>
#include <string>
#include <map>
 
using namespace std;
 
class Fruit {
    private:
        static map<string,Fruit*> types;
        string type;
 
        // note: constructor private forcing one to use static getFruit()
        Fruit(const string& t) : type( t ) {}
 
    public:
        static Fruit* getFruit(const string& type);
        static void printCurrentTypes();
};
 
//declaration needed for using any static member variable
map<string,Fruit*> Fruit::types;        
 
/*
 * Lazy Factory method, gets the Fruit instance associated with a
 * certain type. Instantiates new ones as needed.
 * precondition: type. Any string that describes a fruit type, e.g. "apple"
 * postcondition: The Fruit instance associated with that type.
 */
Fruit* Fruit::getFruit(const string& type) {
    Fruit *& f = types[type];   //try to find a pre-existing instance
 
    if (!f) {
        // couldn't find one, so make a new instance
        f = new Fruit(type); // lazy initialization part
    }
    return f;
}
 
/*
 * For example purposes to see pattern in action
 */
void Fruit::printCurrentTypes() {
    if (types.size() > 0) {
        cout << "Number of instances made = " << types.size() << endl;
        for (map<string,Fruit*>::iterator iter = types.begin(); iter != types.end(); ++iter) {
            cout << (*iter).first << endl;
        }
        cout << endl;
    }
}
 
int main(void) {
    Fruit::getFruit("Banana");
    Fruit::printCurrentTypes();
 
    Fruit::getFruit("Apple");
    Fruit::printCurrentTypes();
 
    // returns pre-existing instance from first 
    // time Fruit with "Banana" was created
    Fruit::getFruit("Banana");
    Fruit::printCurrentTypes();
 
    return 0;
}
 
 
 
/*
OUTPUT:
Number of instances made = 1
Banana
 
Number of instances made = 2
Apple
Banana
 
Number of instances made = 2
Apple
Banana
*/

[edit] SmallTalk

The following is an example (in Smalltalk) of a typical accessor method to return the value of a variable using lazy initialization.

    height
        height ifNil: [height := 2.0].
        ^height

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.

[edit] Ruby

The following 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
    return @auth_token if @auth_token
 
    res = Net::HTTP.post_form(uri, params)
    @auth_token = get_token_from_http_response(res)
  end
 
  # get_token_from_http_response, uri and params are defined later in the class
end
 
b = Blogger.new
b.instance_variable_get(:@auth_token) # returns nil
b.auth_token # returns token
b.instance_variable_get(:@auth_token) # returns token

[edit] See also

[edit] External links

Personal tools