I'm new to Objective-C programming, and creating various classes for an iOS application I'm working on.
When creating objects, it seems like many classes in the built-in frameworks use the "static factory method" pattern, like this:
MyObject* m = [MyObject objectWithName:@"foo" id:@7 description:@"bar"];
however, many classes also simply have overrides on init
, like this:
MyObject* m = [[MyObject alloc] initWithName:@"foo" id:@7 description:@"bar"];
I can see that if I want to cover all my bases, I'd implement both, and have the objectWithName...
method call initWithName...
, however this seems quite tedious.
I was wondering - is there any style or guidance from around when I should implement the factory method pattern vs an init
overload? I've googled for this, but have been unable to find anything (most likely because the terms are quite generic and google doesn't search well for them)
Any advice or opinions would be much appreciated
init
with anautorelease
.