Lazy loading
Encyclopedia
Lazy loading is a design pattern
Design pattern (computer science)
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that...

 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.

Implementations

There are four ways of implementing the lazy load design pattern: lazy initialization; a virtual proxy; a ghost, and a value holder. 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

class SnookerPlayer
{

/**
* @var SnookerCue
*/
protected $_snookerCue;

/**
* Lazy-load snooker cue if one is not assigned to the player
* @return SnookerCue
*/
public function getSnookerCue
{
if ($this->_snookerCue

null) {
$this->_snookerCue = new SnookerCue;
}
return $this->_snookerCue;
}

}

Virtual proxy


Virtual Proxy is an object with the same interface as the real object. The first time one of its methods are called it loads the real object and then delegates.

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
  • Design pattern
    Design pattern (computer science)
    In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that...

  • Proxy
    Proxy pattern
    In computer programming, the proxy pattern is a software design pattern.A proxy, in its most general form, is a class functioning as an interface to something else...

  • Lazy inheritance
    Lazy Inheritance
    Lazy inheritance is a design pattern used in JavaScript computer programming.It designates a postponed linking of an object with its prototype until it is needed...

  • Lazy evaluation
    Lazy evaluation
    In programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until the value of this is actually required and which also avoids repeated evaluations...

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

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK