The lazy loading design mode is designed to avoid unnecessary performance overhead. The so-called delayed loading is when data (read attribute values) is really needed, to load data.
For ease of understanding, let's create a scenario. Suppose we want to construct a Hero (Hero) class. Each Hero has its own name and special skills (SpecialSkill.
Modeling
This is a method of creation:
Copy codeThe Code is as follows: public class Hero
{
Public string FullName {get; set ;}
Public string Name {get; set ;}
Public SpecialSkill Skill {get; set ;}
Public Hero (string name)
{
Name = name;
FullName = "Super" + name;
Skill = new SpecialSkill (name );
}
}
Public class SpecialSkill
{
Public int Power {get; set ;}
Public string SkillName {get; set ;}
Public int StrengthSpent {get; set ;}
Public SpecialSkill (string name)
{
Console. WriteLine ("loading special skill .....");
Power = name. Length;
StrengthSpent = name. Length * 3;
SkillName = name + "Blazing ";
Console. WriteLine (SkillName + ",... this's what makes a legend! ");
}
}
Class Program
{
Static void Main (string [] args)
{
Hero hero = new Hero ("wukong ");
Console. writeLine ("\ n ....................... press Enter to continue ....................... \ n ");
Console. Read ();
Console. WriteLine ("Hero's special skill:" + hero. Skill. SkillName );
Console. Read ();
Console. Read ();
}
}
The output after running the program is as follows. This example is very easy to understand and the result is clear.
Its disadvantage is that when the Hero constructor is run, all attributes of SpecialSkill are loaded. If we only want to get the FullName of Hero, we also load all SpecialSkill values.
Loading delay of attributes
Before Lazy <T> is available, we can do this:
Copy codeThe Code is as follows: public class Hero
{
Public string FullName {get; set ;}
Public string Name {get; set ;}
Private SpecialSkill skill;
Public SpecialSkill Skill
{
Get {return skill ?? (Skill = new SpecialSkill (Name ));}
}
Public Hero (string name)
{
Name = name;
FullName = "Super" + name;
}
}
That is, modify the Loading Method of the SpecialSkill attribute. When we run the program again, the output is:
Very good! This is what we want, so that the system can be more efficient.
Lazy <T>
When the net framework introduces the Lazy <T> class, we can also use it to implement:
Copy codeThe Code is as follows: public class Hero
{
Public string FullName {get; set ;}
Public string Name {get; set ;}
Private readonly Lazy <SpecialSkill> skill;
Public SpecialSkill Skill
{
Get {return skill. Value ;}
}
Public Hero (string name)
{
Name = name;
FullName = "Super" + name;
Skill = new Lazy <SpecialSkill> () => new SpecialSkill (name ));
}
}
Lazy <T> supports delayed initialization. A property Value in Lazy <T> is used to obtain the delay initialization Value of the current Lazy <T> instance.
Advantages of Lazy <T>
Now that we can use the property cache method, why should we introduce Lazy <T>?
At least Lazy <T> has the following advantages:
It has LazyThreadSafetyMode, But we generally do not use it unless it is critical (181 words are omitted here)
It makes attribute definition rows easier.
In terms of semantics, it is clearer and more readable.
It allows null to be a valid value.