Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Lazy initialization
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===C#=== In .NET Framework 4.0 Microsoft has included a <code>Lazy</code> class that can be used to do lazy loading. Below is some dummy code that does lazy loading of Class <code>Fruit</code> <syntaxhighlight lang="csharp"> var lazyFruit = new Lazy<Fruit>(); Fruit fruit = lazyFruit.Value; </syntaxhighlight> Here is a dummy example in [[C Sharp (programming language)|C#]]. The <code>Fruit</code> class itself doesn't do anything here, The [[class variable]] <code>_typesDictionary</code> is a Dictionary/Map used to store <code>Fruit</code> instances by <code>typeName</code>. <syntaxhighlight lang="csharp"> using System; using System.Collections; using System.Collections.Generic; public class Fruit { private string _typeName; private static IDictionary<string, Fruit> _typesDictionary = new Dictionary<string, Fruit>(); private Fruit(string typeName) { this._typeName = typeName; } public static Fruit GetFruitByTypeName(string type) { Fruit fruit; if (!_typesDictionary.TryGetValue(type, out fruit)) { // Lazy initialization fruit = new Fruit(type); _typesDictionary.Add(type, fruit); } 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(); } } public Fruit() { // required so the sample compiles } } 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(); } } </syntaxhighlight> A fairly straightforward 'fill-in-the-blanks' example of a Lazy Initialization design pattern, except that this uses an [[Enumerated type|enumeration]] for the type <syntaxhighlight lang="csharp"> namespace DesignPatterns.LazyInitialization; public class LazyFactoryObject { // internal collection of items // IDictionary makes sure they are unique private IDictionary<LazyObjectSize, LazyObject> _LazyObjectList = new Dictionary<LazyObjectSize, LazyObject>(); // enum for passing name of size required // avoids passing strings and is part of LazyObject ahead public enum LazyObjectSize { None, Small, Big, Bigger, Huge } // standard type of object that will be constructed public struct LazyObject { public LazyObjectSize Size; public IList<int> Result; } // takes size and create 'expensive' list private IList<int> Result(LazyObjectSize size) { IList<int> result = null; switch (size) { case LazyObjectSize.Small: result = CreateSomeExpensiveList(1, 100); break; case LazyObjectSize.Big: result = CreateSomeExpensiveList(1, 1000); break; case LazyObjectSize.Bigger: result = CreateSomeExpensiveList(1, 10000); break; case LazyObjectSize.Huge: result = CreateSomeExpensiveList(1, 100000); break; case LazyObjectSize.None: result = null; break; default: result = null; break; } return result; } // not an expensive item to create, but you get the point // delays creation of some expensive object until needed private IList<int> CreateSomeExpensiveList(int start, int end) { IList<int> result = new List<int>(); for (int counter = 0; counter < (end - start); counter++) { result.Add(start + counter); } return result; } public LazyFactoryObject() { // empty constructor } public LazyObject GetLazyFactoryObject(LazyObjectSize size) { // yes, i know it is illiterate and inaccurate LazyObject noGoodSomeOne; // retrieves LazyObjectSize from list via out, else creates one and adds it to list if (!_LazyObjectList.TryGetValue(size, out noGoodSomeOne)) { noGoodSomeOne = new LazyObject(); noGoodSomeOne.Size = size; noGoodSomeOne.Result = this.Result(size); _LazyObjectList.Add(size, noGoodSomeOne); } return noGoodSomeOne; } } </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Lazy initialization
(section)
Add topic