
Objective-C Singleton Pattern Updated For Testability
At Two Bit Labs we do a fair amount of unit testing. In places where we use singletons we use a variation on the the Objective-C dispatch_once pattern of thread safe singleton creation. This variation supports resetting the singleton or replacing it with a mock object. That way in our unit tests we can do […]
Mocking singletons with OCMock
Although Graham Lee might not approve, I occasionally use the singleton pattern, particularly for managing access to my data models. Singletons in Cocoa are accessed using the following convention: static UserModelManager *sharedManager = nil; +(UserModelManager *)sharedManager { if (sharedManager == nil) { sharedManager = [[super allocWithZone:NULL] init]; […]