Lazy initialization
The decision to not instantiate an object until it is needed is called lazy initialization. The word lazy normally has a negative connotation; however, in the context of software development, lazy initialization is a performance optimization approach with the goal of managing an application’s overhead. This design pattern is especially useful when detailing very large objects.
Should I implement lazy initialization?
If you have string objects that are large or complex and require significant overhead at initialization time, then you should consider lazy initialization.
Lazy initialization does not reduce the number of string objects created; rather, it delays the initialization until it is required by your application for processing. This delaying technique can help you with memory optimization. Let us look at a narrative example before we examine the source code.
Imagine you have a legacy application that uses very sophisticated string objects...