Science Fair Projects Ideas - Lazy initialization

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

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:

  • using a factory method to get instances of a class (factory method pattern)
  • storing the instances in a map, so you get the same instance the next time you ask for an instance with some parameter (compare with a singleton pattern)
  • using lazy initialization to instantiate the object the first time it is requested (lazy initialization pattern)

Here is a dummy example (in Java). 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);
    }

    /**
     * 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 Fruit getFruit(String type){
        Fruit f;
        if (types.containsKey(type)){
            f = (Fruit) types.get(type); // get the instance for that type   
        } else {
            f = new Fruit(type); // lazy initialization
        }
        return f;
    }
}

Note that the technique can also be used in non-object-oriented languages.

Last updated: 08-28-2005 23:15:07
12-03-2008 10:22:39
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice