In the builder pattern one creates a new objects using a dedicated builder object allowing you to avoid having to make multiple constructors, and greatly increasing the readability of your code when initializing complex objects.
This example from Klass Pieter Annema illustrates the builder pattern well with some code such as this:
Becomes this:
builder.size = 12;
builder.pepperoni = YES;
builder.mushrooms = YES;
Pizza *pizza = [builder build];
And using a configuration block can become even simpler:
builder.size = 12;
builder.pepperoni = YES;
builder.mushrooms = YES;
}];
You can read more about implementing the builder pattern in Objective-C over on Klaas Pieter Annema’s site, and on Joris Kluivers’ site.
You can find Joris’ Kluivers NSDate and NSURL example builder categories on Github here.
A nice set of guides and examples on implementing the builder pattern in Objective-C.
- Tutorial: How To Replace The Standard Objective-C Delegate Pattern With ReactiveCocoa Signals
- Open Source Android Style Pattern To Unlock Control For iOS
- Top iOS Development Resources – For Week Ended December 16th, 2012
- Video Introduction To ReactiveCocoa And Tutorial On Reactive Cocoa In A Data-Driven Application
- Open Source: Active Record Library For SQLite Databases In iOS Apps
Original article: Guide And Examples: Simplifying Code Using The Builder Pattern In Objective-C
©2014 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.




