Cocoa Design Patterns

A copy of Cocoa Design Patterns by Erik M. Buck and Donald A. Yacktman arrived from Amazon this morning and I am already liking it a lot. I would not say it is the first book you should read about Cocoa programming but it may well be the second.

Note that this is not a book about iPhone programming though a lot of the material applies directly to Cocoa Touch on the iPhone. So don’t expect to find a detailed discussion of UIKit or other iPhone specific APIs. What it does provide are a large number of best practises from some experienced Cocoa developers. Aaron Hillegass sums it up nicely in the foreword:

Through floundering about with bad solutions, grumpy old Cocoa programmers have figured out some really good solutions to common design problems. The existence of this book means that you are not required to suffer through the same misery that we went through.

The book does assume some basic knowledge of object-oriented programming and if not Objective C than at least some C/C++ or Java. But in my view this book is perfect for somebody who has come to Cocoa via the iPhone and now wants to really understand how to get the most out the framework.

The first part of the book is a detailed discussion of the Model View Controller (MVC) architecture. If you were not already convinced that you should be using MVC in your applications they walk thought a simple application written with and without it.

The second part of the book steps through some fundamental Cocoa patterns such as two stage creation (alloc, init), categories, accessors, etc. Each pattern explains the motivation for creating the pattern (what the problem is), a detailed description of the pattern (how it solves the problem), examples of using the pattern and finally the consequences or what you should be aware of if you apply the pattern.

The third and fourth parts of the book cover patterns that either help to decouple your code (that is reduce dependencies between classes) or help to reduce complexity. Examples include the singleton pattern, delegates, the responder chain, bundles, class clusters, etc. There is some real depth here with good explanations and examples that make it easy to follow.

The final part of the book covers some of the tools that help apply patterns and includes some discussion on the use of core data models, views, binding and controllers.

In summary I highly recommend this book if you really want to understand Cocoa. Applying the patterns it describes can make a big difference to your application and I think it does a good job of revealing some of the more arcane parts of Cocoa.