Lazy loading

Lazy loading

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.

Implementations

There are four ways of implementing the lazy load design pattern: "lazy initialization"; a "virtual proxy"; a "ghost" and a "value holder" [Martin Fowler, "Patterns of Enterprise Application Architecture", Addison-Wesley, 2003, pp.200-214. ISBN 0-321-12742-0.] . 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.

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

* Design pattern
* Proxy
* Lazy Inheritance
* lazy evaluation

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Lazy Inheritance — is a design pattern used in JavaScript computer programming.It designates a postponed linking of an object with its prototype (class) until it is needed. If used properly, such approach may increase efficiency, simplicity and flexibility of OOP… …   Wikipedia

  • Loading (disambiguation) — Loading is the insertion of impedance into a circuit to change the characteristics of the circuit.Loading may also refer to:* Carbohydrate loading, a strategy employed by endurance athletes to maximize the storage of glycogen in the muscles *… …   Wikipedia

  • 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. This is typically accomplished by maintaining a flag… …   Wikipedia

  • Lazy evaluation — In computer programming, lazy evaluation (or delayed evaluation) is the technique of delaying a computation until such time as the result of the computation is known to be needed.The actions of lazy evaluation include: performance increases due… …   Wikipedia

  • Granite data services — Infobox Software name = Granite data services caption = collapsible = author = developer = released = latest release version = latest release date = latest preview version = latest preview date = frequently updated = programming language =… …   Wikipedia

  • Demand paging — In computer operating systems, demand paging (as opposed to anticipatory paging) is an application of virtual memory. In a system that uses demand paging, the operating system copies a disk page into physical memory only if an attempt is made to… …   Wikipedia

  • Singleton pattern — In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is needed to… …   Wikipedia

  • Hibernate (Framework) — Hibernate Entwickler JBoss (Red Hat) Aktuelle Version 3.6.8 (27. Oktober 2011) Aktuelle Vor …   Deutsch Wikipedia

  • Identity map — This article is about the Identity Map software design pattern. For the mathematical concept, see Identity function. An identity map is a database access design pattern used to improve performance by providing a context specific in memory cache… …   Wikipedia

  • Datamapper — is an object relational mapper library written in Ruby and commonly used with Merb. It was developed to address perceived shortcomings in Ruby on Rails ActiveRecord library. Some features of Datamapper:[1] eager loading of child associations to… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”