Wednesday, November 21, 2012

Lazy initialization

  • 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.

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 (Multiton pattern, similar to the singleton pattern)
    using lazy initialization to instantiate the object the first time it is requested (lazy initialization pattern).

http://en.wikipedia.org/wiki/Lazy_initialization#Java





  • Lazy initialization is a performance optimization.

if the hashCode value for an object might not actually be needed by its caller, always calculating the hashCode for all instances of the object may be felt to be unnecessary.
since accessing a file system or network is relatively slow, such operations should be put off until they are absolutely required.

Lazy initialization has two objectives :

    delay an expensive operation until it's absolutely necessary
    store the result of that expensive operation, such that you won't need to repeat it again

http://www.javapractices.com/topic/TopicAction.do?Id=34

No comments:

Post a Comment