热门问题
时间线
聊天
视角

惰性初始化

来自维基百科,自由的百科全书

Remove ads

程式設計中, 惰性初始化(英語:lazy initialization)是一種拖延戰術。在第一次需求出現以前,先延遲創建物件、計算值或其它昂貴程序。這通常是以一個旗號來實現,用旗號來標示是否完成其程式。每次請求對象時,會先測試此旗號。如果已完成,直接傳回,否則當場執行。

對於此想法更一般的論述,可見惰性求值。對指令式語言,這個模式可能潛藏著危險,尤其是使用共享狀態的程式習慣。

惰性工廠

設計模式》書中的工廠方法模式C++實現[1],由於這裡的工廠方法CreateProduct()總是虛函數並且經常是純虛函數Creator在其構造子中只將具體產品初始化是為0,轉而通過其訪問子來返回這個產品;訪問子GetProduct()在需要的時候創建這個產品,《設計模式》書中將這種技術稱為「惰性初始化」:

class Creator {
public:
    Creator();
    Product* GetProduct();
protected:
    virtual Product* CreateProduct();
private:
    Product* _product;
};
Creator::Creator() {
    _product = 0;
}
Product* Creator::GetProduct() {
    if (_product == 0) {
        _product = CreateProduct();
    }
    return _product;
}

這種「惰性工廠」結合了二個構想:

在軟件設計實踐中經常還會結合上第三個構想:

  • 多例模式英語Multiton pattern:將實例存儲在一個映射中,在以「相同」的參數要求一個實例之時,返回「同一個」實例。
Remove ads

理論計算機科學

理論計算機科學領域中,惰性初始化(也叫做惰性數組)[2],是設計數據結構的技術,使其可以工作於不需要被初始化的內存。尤其是假定了要訪問n個(編號從 1n)未初始化內存單元的一個表格T,並希望賦值這個數組的m個單元,比如賦值T[ki] := vi,它針對鍵值對(k1, v1), ..., (km, vm),並具有所有的ki都是不同的。惰性初始化技術允許只用O(m)次運算來完成它,而非耗費O(m+n)次運算來首先初始化所有數組單元。這項技術簡單的分配一個表格V以任意次序存儲鍵值對(ki, vi),並為每個i在單元T[ki]中寫入鍵ki存儲在V中的位置,保留T的其他單元不初始化。這可以用來處理下列方式的查詢:在針對某個k查看單元T[k]的時候,可以在{1, ..., m}範圍內檢查T[k];如果不在其中,則T[k]是未初始化的。否則檢查V[T[k]],並驗證這個鍵值對的第一個組件等於k;如果不等於,則T[k]是未初始化的(它只是意外落入範圍{1, ..., m}中);否則確知了T[k]是初始化的單元之一,而對應的值是這個鍵值對的第二個組件。

Remove ads

示例

C#

在下面的C#例子中,Fruit 類別本身在這裡不做任合事。_typesDictionary 變數則是一個存 Fruit 實例的 Dictionary 或 Map ,其透過typeName來存取。

using System;
using System.Collections;
using System.Collections.Generic;

public class Fruit {
    private string _typeName;
    private static Dictionary<string, Fruit> _typesDictionary = new Dictionary<string, Fruit>();

    private Fruit(String typeName) {
        this._typeName = typeName;
    }

    public static Fruit GetFruitByTypeName(string type) {
        Fruit fruit;

        if (!_typesDictionary.ContainsKey(type)) {
            // 惰性初始
            fruit = new Fruit(type);
            _typesDictionary.Add(type, fruit);
        }
        else
            fruit = _typesDictionary[type];
        return fruit;
    }

    public static void ShowAll() {
        if (_typesDictionary.Count > 0) {
            Console.WriteLine("Number of instances made = {0}", _typesDictionary.Count);
            foreach (KeyValuePair<string, Fruit> kvp in _typesDictionary) {
                Console.WriteLine(kvp.Key);
            }
            Console.WriteLine();
        }
    }
}

class Program {
    static void Main(string[] args) {
        Fruit.GetFruitByTypeName("Banana");
        Fruit.ShowAll();

        Fruit.GetFruitByTypeName("Apple");
        Fruit.ShowAll();

        // returns pre-existing instance from first 
        // time Fruit with "Banana" was created
        Fruit.GetFruitByTypeName("Banana");
        Fruit.ShowAll();

        Console.ReadLine();
    }
}
Remove ads

Java

Java例子:

import java.util.*;
  
public class Fruit {
    private static final Map<String,Fruit> types = new HashMap<String,Fruit>();
    private final String type;
  
    // using a private constructor to force use of the factory method.
    private Fruit(String type) {
      this.type = type;
    }
    
    /**
     * Lazy Factory method, gets the Fruit instance associated with a
     * certain type. Instantiates new ones as needed.
     * @param type Any string that describes a fruit type, e.g. "apple"
     * @return The Fruit instance associated with that type.
     */
    public static synchronized Fruit getFruit(String type) {
      if(!types.containsKey(type))
        types.put(type, new Fruit(type)); // Lazy initialization
      return types.get(type);
    }
}
Remove ads

C++

C++的例子:

import std;

class Fruit {
private:
    static std::map<std::string, Fruit*> types;
    std::string type_;

    // Note: constructor private forcing one to use static |GetFruit|.
    Fruit(const std::string& type): 
        type_{type} {}
public:
    // Lazy Factory method, gets the |Fruit| instance associated with a certain
    // |type|.  Creates new ones as needed.
    static Fruit* getFruit(const std::string& type) {
        auto [it, inserted] = types.emplace(type, nullptr);
        if (inserted) {
            it->second = new Fruit(type);
        }
        return it->second;
    }

    // For example purposes to see pattern in action.
    static void printCurrentTypes() {
        std::println("Number of instances made = {}", types.size());
        for (const auto& [type, fruit] : types) {
            std::println({}, type);
        }
        std::println();
    }
};

// static
std::map<std::string, Fruit*> Fruit::types;

int main(int argc, char* argv[]) {
    Fruit::getFruit("Banana");
    Fruit::printCurrentTypes();

    Fruit::getFruit("Apple");
    Fruit::printCurrentTypes();

    // Returns pre-existing instance from first time |Fruit| with "Banana" was
    // created.
    Fruit::getFruit("Banana");
    Fruit::printCurrentTypes();
}

// OUTPUT:
//
// Number of instances made = 1
// Banana
//
// Number of instances made = 2
// Apple
// Banana
//
// Number of instances made = 2
// Apple
// Banana
//
Remove ads

Python

Python的對象惰性實例化例子:

from abc import ABCMeta
from weakref import WeakValueDictionary

class Fruit(metaclass=ABCMeta):
    def __init__(self, name, **kwargs):
        self.name = name
        for key, value in kwargs.items():
            self.__dict__[key] = value

class FruitFuture():
    weakref_dict = WeakValueDictionary()
    count = 0
    def __init__(self, name, **kwargs):
        self.name = name
        self.kwargs = kwargs
        type(self).count += 1
    def result(self):
        if id(self) in type(self).weakref_dict:
            fruit = type(self).weakref_dict[id(self)]
        else:
            fruit = Fruit(self.name, **self.kwargs)
            type(self).weakref_dict[id(self)] = fruit
        return fruit
    @classmethod
    def info(cls):
        print(f'Instances created:   {cls.count}')
        print(f'Instances exist now: {len(cls.weakref_dict)}')

Fruit.register(FruitFuture)

這裡實現了創建產品的Future,略過了可附加的多例模式英語Multiton pattern,它與享元模式中類似例子的區別在於,它的目標是推延而非共享,下面是其執行:

>>> fruit1 = FruitFuture('Banana')
>>> assert isinstance(fruit1, Fruit)
>>> fruit2 = FruitFuture('Apple')
>>> FruitFuture.info()
Instances created:   2
Instances exist now: 0
>>> fruit1 = fruit1.result()
>>> fruit2 = fruit2.result()
>>> FruitFuture.info()
Instances created:   2
Instances exist now: 2
>>> fruit2 = FruitFuture('Banana').result()
>>> FruitFuture.info()
Instances created:   3
Instances exist now: 2

Python的對象惰性初始化例子[3]

import functools

class lazyinitialize():
    def __init__(self, f):
        self.f = f
        functools.update_wrapper(self, f)
    def __get__(self, obj, objtype=None):
        if obj is None: 
            #Invocation from a class
            return self
        val = self.f(obj)
        obj.__dict__[self.f.__name__] = val
        return val

class Employee():
    def __init__(self, name, employer):
        self.name = name
        self.employer = employer
    @lazyinitialize
    def report(self):
        #Spending a lot of time and effort to get a result
        result = "Voluminous document: 100,000 words omitted here."
        return result

其執行:

>>> employee = Employee("Taciturn Man", "Large Institution")
>>> employee.report
'Voluminous document: 100,000 words omitted here.'
>>> employee.report = "Concise documentation."
>>> employee.report
'Concise documentation.'
Remove ads

Smalltalk

下面的Smalltalk例子,具有典型的訪問子方法,它返回使用惰性初始化的一個變量的值。

height
    ^height ifNil: [height := 2.0].

非惰性的替代者,使用的初始化方法是在對象被創建時運行的,並且使用簡單的訪問子方法來取回這個值。

initialize
    height := 2.0

height
    ^height

注意惰性初始化也可以用在非面向對象編程語言中。

Ruby

下面的Ruby例子,具有惰性初始化的來自遠程服務的身份驗證令牌@auth_token被緩存的方式也是記憶化的示例。

require 'net/http'
class Blogger
  def auth_token
    @auth_token ||=
      (res = Net::HTTP.post_form(uri, params)) &&
      get_token_from_http_response(res)
  end

  # get_token_from_http_response, uri and params are defined later in the class
end

b = Blogger.new
b.instance_variable_get(:@auth_token) # returns nil
b.auth_token # returns token
b.instance_variable_get(:@auth_token) # returns token

PHP

PHP 7.4的惰性初始化的例子:

<?php
header('Content-Type: text/plain; charset=utf-8');

class Fruit
{
    private string $type;
    private static array $types = array();

    private function __construct(string $type)
    {
        $this->type = $type;
    }

    public static function getFruit(string $type): Fruit
    {
        // Lazy initialization takes place here
        if (!isset(self::$types[$type])) {
            self::$types[$type] = new Fruit($type);
        }

        return self::$types[$type];
    }

    public static function printCurrentTypes(): void
    {
        echo 'Number of instances made: ' . count(self::$types) . "\n";
        foreach (array_keys(self::$types) as $key) {
            echo "$key\n";
        }
        echo "\n";
    }
}

Fruit::getFruit('Apple');
Fruit::printCurrentTypes();

Fruit::getFruit('Banana');
Fruit::printCurrentTypes();

Fruit::getFruit('Apple');
Fruit::printCurrentTypes();

/*
OUTPUT:

Number of instances made: 1
Apple

Number of instances made: 2
Apple
Banana

Number of instances made: 2
Apple
Banana
*/
Remove ads

另見

引用

外部連結

Loading related searches...

Wikiwand - on

Seamless Wikipedia browsing. On steroids.

Remove ads