Chapter 21. Lazy Initialization Pattern

The lazy initialization pattern embodies the just-in-time philosophy of data delivery. It delays the construction of values or data structures until that data is actually needed. Lazy initialization is a popular design pattern in both Java and Objective-C. Objective-C adds another level by implementing its own lazy initialization of class structures, which you can integrate into your own classes.

Implementing the Pattern

Here's how to implement the lazy initialization pattern:

  1. Create a private instance or private static variable that will eventually contain the data, but is initialized with a placeholder value (typically nil).

  2. Wrap the variable in a property that provides a public getter method.

  3. In the getter ...

Get Learn Objective-C for Java Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.