지연된 초기화

지연된 초기화, 게으른 초기화(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.
함수에서:
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-&gt;next)
 if (0 == strcmp(name, f-&gt;name))
 return f;
 if (!(f = malloc(sizeof(struct fruit))))
 return NULL;
 if (!(f-&gt;name = strdup(name))) {
 free(f);
 return NULL;
 f-&gt;number = ++seq;
 f-&gt;next = fruit_list;
 fruit_list = f;
 return f;
/* Example code */
int main(int argc, char *argv[]) {
 int i;
 struct fruit *f;
 if (argc &lt; 2) {
 fprintf(stderr, "Usage: fruits fruit-name [...]\n");
 exit(1);
 for (i = 1; i &lt; argc; i++) {
 if ((f = get_fruit(argv[i]))) {
 printf("Fruit %s: number %d\n", argv[i], f-&gt;number);
 return 0;
fruit.h:
struct fruit {
 char *name;
 struct fruit *next;
 int number;
 /* Other members */
struct fruit *get_fruit(char *name);
void print_fruit_list(FILE *file);
fruit.c:
static struct fruit *fruit_list;
static int seq;
struct fruit *get_fruit(char *name) {
 struct fruit *f;
 for (f = fruit_list; f; f = f-&gt;next)
 if (0 == strcmp(name, f-&gt;name))
 return f;
 if (!(f = malloc(sizeof(struct fruit))))
 return NULL;
 if (!(f-&gt;name = strdup(name))) {
 free(f);
 return NULL;
 f-&gt;number = ++seq;
 f-&gt;next = fruit_list;
 fruit_list = f;
 return f;
void print_fruit_list(FILE *file) {
 struct fruit *f;
 for (f = fruit_list; f; f = f-&gt;next)
 fprintf(file, "%4d %s\n", f-&gt;number, f-&gt;name);
main.c:
int main(int argc, char *argv[]) {
 int i;
 struct fruit *f;
 if (argc &lt; 2) {
 fprintf(stderr, "Usage: fruits fruit-name [...]\n");
 exit(1);
 for (i = 1; i &lt; argc; i++) {
 if ((f = get_fruit(argv[i]))) {
 printf("Fruit %s: number %d\n", argv[i], f-&gt;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&lt;String, Fruit&gt;();
 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"]