Book Image

Objective C Memory Management Essentials

Book Image

Objective C Memory Management Essentials

Overview of this book

Table of Contents (18 chapters)
Objective-C Memory Management Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

The singleton pattern


Besides taking responsibility for garbage management, a good programmer should also be aware of programming design patterns. Design patterns are solutions, mostly reusable code solutions, to solve and prevent common issues. It makes a developer's life easier. In this section, I'll show you the singleton pattern. Singletons are useful if you need a single instance and need to manage that single instance such as writing to a log file. However, singletons can be misused as global variables, which makes for bad programming practice. Singletons are also implemented using static methods, which is not good for unit testing as they cannot be mocked or stubbed. So, only use a singleton in the correct context and not in every situation that you encounter.

In Objective-C, it's completely possible to have more than one instance of a class (objects) at a time. However, what if you don't need it? What if, for some reason, you need only one instance and nothing more and want to avoid...