I think the Singleton example in the book could use a bit more explanation. While I know the Singleton Pattern from my experience in AS3, the implementation in the book has left me with a couple of questions.
One of those questions has already been answered on the forums, namely why you would ‘cripple’ allocWithZone instead of alloc.
If I understand correctly, by doing this you’re still allowing people to call our Singleton class by using
PossessionStore *store = [[PossessionStore alloc] init];
you’re just changing it so that it will only give you a non-retained version of PossessionStore, the same way the class method + (PosessionStore *)defaultStore works. But why not just cripple the init function too and only allow it to throw an error or just do nothing? This would make it more obvious to other ‘classes’ or people using our class that they’re working with a Singleton.
Also, the NSMutableArray ‘allPossessions’ is initialized in the init function of our Singleton, not in our defaultStore class method. So if we would not be initialized if another class would just ask for our instance by sending the defaultStore message?
Or I just might be completely wrong in interpreting this and would love to be corrected.
Either way, I think this is one of the subjects that could have been explained more thoroughly in the book.