Share on Facebook Share on Twitter Email
Answers.com

Lazy loading

 
Computer Desktop Encyclopedia: dynamic function loading

The capability to specify which routines (modules, objects, etc.) are called into memory when a program is first launched. Also known as "lazy loading." Comprehensive applications may contain dozens or even hundreds of features that may never be used by most people. Limiting the number of functions called in at startup reduces the initial load time.

Download Computer Desktop Encyclopedia to your iPhone/iTouch

Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics
Wikipedia: Lazy loading
Top

Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program's operation if properly and appropriately used. The opposite of lazy loading is Eager Loading.

Contents

Implementations

There are four ways of implementing the lazy load design pattern: lazy initialization; a virtual proxy; a ghost and a value holder[1]. Each has its own advantages and disadvantages.

Lazy initialization

With lazy initialization, the object to be lazily loaded is originally set to null, and every request for the object checks for null and creates it "on the fly" before returning it first, as in this C# example:

 private int _myWidgetID;
 private Widget _myWidget = null;
 
 public Widget MyWidget {
   get {
     if (_myWidget == null) {
       _myWidget = Widget.Load(_myWidgetID);
     }
     return _myWidget;
   }
 }

This method is the simplest to implement, although if null is a legitimate return value, it may be necessary to use a placeholder object to signal that it has not been initialized.

Here is an example using PHP v5

class SnookerPlayer
{
 
    /**
     * @var SnookerCue
     */
    protected $_snookerCue;
 
    /**
     * Lazy-load snooker cue if one is not assigned to the player
     * @return SnookerCue
     */
    public function getSnookerCue()
    {
        if (is_null($this->_snookerCue)) {
            $this->_snookerCue = new SnookerCue();
        }
        return $this->_snookerCue;
    }
 
}

Virtual proxy

A virtual proxy is an object that "looks like" the object that is to be lazily loaded. Both the object and the proxy implement the same interface, with the proxy providing a wrapper that instantiates and initializes the object when one of its properties is accessed for the first time.

Ghost

A ghost is the object that is to be loaded in a partial state. It may only contain the object's identifier, but it loads its own data the first time one of its properties is accessed.

Value holder

A value holder is a generic object that handles the lazy loading behavior, and appears in place of the object's data fields:

 private ValueHolder _myWidget;
 
 public Widget MyWidget {
   get {
     return (Widget)_myWidget.GetValue();
   }
 }

See also

References

  1. ^ Martin Fowler, Patterns of Enterprise Application Architecture, Addison-Wesley, 2003, pp.200-214. ISBN 0-321-12742-0.

 
 
Learn More
Identity map
Granite data services
Loading (disambiguation)

What animal is lazy? Read answer...
What is lazy yoga? Read answer...
What is lazy eye? Read answer...

Help us answer these
How do you know if you are lazy?
Why are camels lazy?
Synonyms for lazy?

Post a question - any question - to the WikiAnswers community:

 

Copyrights:

Computer Desktop Encyclopedia. THIS DEFINITION IS FOR PERSONAL USE ONLY.
All other reproduction is strictly prohibited without permission from the publisher.
© 1981-2010 The Computer Language Company Inc.  All rights reserved.  Read more
Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "Lazy loading" Read more