Pattern: Lazy
Description
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.
Example in .NET :
Lazy
namespace Lazy; internal class LazyDemo { public DateTime dateTimeConstructClass =DateTime.Now; public Lazy<DateTime> DateTimeLazy = new(() => { Console.WriteLine("Lazy<DateTime> is being initialized ONCE!"); return DateTime.Now; }); }
Learn More
Source Code for Microsoft implementation of Lazy
Learn More
Homework
Implement a lazy initialization of a logger that logs to a file and to a console. The logger should be created only when it is needed.
Leave a Reply