지연된 초기화

지연된 초기화, 게으른 초기화(lazy initialization)은 컴퓨터 프로그래밍에서 객체 생성, 값 계산, 또는 일부 기타 비용이 많이 드는 과정을 처음 필요한 시점까지 지연시키는 기법이다.

액션스크립트 3

package examples.lazyinstantiation{
public class Fruit
{
private var _typeName:String;
private static var instancesByTypeName:Dictionary = new Dictionary();

public function Fruit(typeName:String):void
{
this._typeName = typeName;
}

public function get typeName():String
{
return _typeName;
}

public static function getFruitByTypeName(typeName:String):Fruit
{
return instancesByTypeName[typeName] ||= new Fruit(typeName);
}

public static function printCurrentTypes():void
{
for each (var fruit:Fruit in instancesByTypeName)
{
// iterates through each value
trace(fruit.typeName);
}
}
}}
기본 사용법:
package{
import examples.lazyinstantiation;

public class Main
{
public function Main():void
{
Fruit.getFruitByTypeName("Banana");
Fruit.printCurrentTypes();

Fruit.getFruitByTypeName("Apple");
Fruit.printCurrentTypes();

Fruit.getFruitByTypeName("Banana");
Fruit.printCurrentTypes();
}
}}

C

함수에서:
#include <string.h>#include <stdlib.h>#include <stddef.h>#include <stdio.h>struct fruit {
    char *name;
    struct fruit *next;
    int number;
    /* Other members */};struct fruit *get_fruit(char *name) {
    static struct fruit *fruit_list;
    static int seq;
    struct fruit *f;
    for (f = fruit_list; f; f = f->next)
        if (0 == strcmp(name, f->name))
            return f;
    if (!(f = malloc(sizeof(struct fruit))))
        return NULL;
    if (!(f->name = strdup(name))) {
        free(f);
        return NULL;
    }
    f->number = ++seq;
    f->next = fruit_list;
    fruit_list = f;
    return f;}/* Example code */int main(int argc, char *argv[]) {
    int i;
    struct fruit *f;
    if (argc < 2) {
        fprintf(stderr, "Usage: fruits fruit-name [...]\n");
        exit(1);
    }
    for (i = 1; i < argc; i++) {
        if ((f = get_fruit(argv[i]))) {
            printf("Fruit %s: number %d\n", argv[i], f->number);
        }
    }
    return 0;}
fruit.h:
#ifndef _FRUIT_INCLUDED_#define _FRUIT_INCLUDED_struct fruit {
    char *name;
    struct fruit *next;
    int number;
    /* Other members */};struct fruit *get_fruit(char *name);void print_fruit_list(FILE *file);#endif /* _FRUIT_INCLUDED_ */
fruit.c:
#include <string.h>#include <stdlib.h>#include <stddef.h>#include <stdio.h>#include "fruit.h"static struct fruit *fruit_list;static int seq;struct fruit *get_fruit(char *name) {
    struct fruit *f;
    for (f = fruit_list; f; f = f->next)
        if (0 == strcmp(name, f->name))
            return f;
    if (!(f = malloc(sizeof(struct fruit))))
        return NULL;
    if (!(f->name = strdup(name))) {
        free(f);
        return NULL;
    }
    f->number = ++seq;
    f->next = fruit_list;
    fruit_list = f;
    return f;}void print_fruit_list(FILE *file) {
    struct fruit *f;
    for (f = fruit_list; f; f = f->next)
        fprintf(file, "%4d  %s\n", f->number, f->name);}
main.c:
#include <stdlib.h>#include <stdio.h>#include "fruit.h"int main(int argc, char *argv[]) {
    int i;
    struct fruit *f;
    if (argc < 2) {
        fprintf(stderr, "Usage: fruits fruit-name [...]\n");
        exit(1);
    }
    for (i = 1; i < argc; i++) {
        if ((f = get_fruit(argv[i]))) {
            printf("Fruit %s: number %d\n", argv[i], f->number);
        }
    }
    printf("The following fruits have been generated:\n");
    print_fruit_list(stdout);
    return 0;}

Haxe

Haxe의 예는 다음과 같다:
class Fruit {
  private static var _instances = new Map<String, Fruit>();

  public var name(default, null):String;

  public function new(name:String) {
    this.name = name;
  }

  public static function getFruitByName(name:String):Fruit {
    if (!_instances.exists(name)) {
      _instances.set(name, new Fruit(name));
    }
    return _instances.get(name);
  }

  public static function printAllTypes() {
    trace([for(key in _instances.keys()) key]);
  }}
사용법
class Test {
  public static function main () {
    var banana = Fruit.getFruitByName("Banana");
    var apple = Fruit.getFruitByName("Apple");
    var banana2 = Fruit.getFruitByName("Banana");

    trace(banana == banana2); // true. same banana

    Fruit.printAllTypes(); // ["Banana","Apple"]
  }}