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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.