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 indicating whether the process has taken place. Each time the desired thing is summoned, the flag is tested. If it s ready, it is returned. If not, it is initialized on the spot.
=The lazy factory =
In a software design pattern view, lazy initialization is often used together with a factory method pattern. This combines three ideas:
Here is a dummy example (in Java programming language). The Fruit class itself doesn t do anything here, this is just an example to show the architecture. The class variable types is a map used to store Fruit instances by type.
import java.util.*; public class Fruit { private static Map types = new HashMap(); private String type; // using a private constructor to force use of the factory method. private Fruit(String type){ this.type=type; types.put(type, this); } /**
|
|