请输入您要查询的百科知识:

 

词条 Lazy loading
释义

  1. Implementations

      Lazy initialization    Virtual proxy    Ghost    Value holder  

  2. See also

  3. References

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. The performance gains are especially significant if the initialization of the object is costly, such as in case of accessing network services. This makes it ideal in use cases where network content is accessed and initialization times are to be kept at a minimum, such as in the case of web pages.

Implementations

There are four common 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

{{Main|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;    }

}

Or with the null-coalescing operator '??'

private int myWidgetID;

private Widget myWidget = null;

public Widget 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. If this method is used in a multithreaded application, synchronization must be used to avoid race conditions.

Virtual proxy

Virtual Proxy is an object with the same interface as the real object. The first time one of its methods is 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. For example, consider that a user is about to request content via an online form. At the time of creation all we know is that content will be accessed but what action or content is unknown.

PHP Example:

$userData = array (

    "UID" = > uniqid(),    "requestTime" => microtime( true ),    "dataType" => "",    "request" => ""

);

if (isset( $_POST['data'] ) && $userData) {

}

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 valueHolder;

public Widget MyWidget

{
    get    {        return valueHolder.GetValue();    }

}

See also

  • Design pattern
  • Proxy
  • Lazy inheritance
  • Lazy evaluation
  • Lazy initialization

References

1. ^{{Cite book|author=Martin Fowler|title=Patterns of Enterprise Application Architecture|publisher=Addison-Wesley|date=2003|pages=200–214|isbn=0-321-12742-0}}
{{Design Patterns patterns}}{{compu-prog-stub}}

1 : Software design patterns

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/17 22:57:51