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

SourceCode Lazy

Learn More

C2Wiki
Wikipedia

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.