alternate pattern for object initializers
alternate pattern for object initializers
- Subject: alternate pattern for object initializers
- From: Stuart Malin <email@hidden>
- Date: Sun, 25 May 2008 19:47:41 -1000
I am reasonably new to Cocoa. I do have the concept of a "designated
initializer" understood. However, I have begun to use an alternate
pattern. Before I get too far in my use of this approach, I thought
it best to check with the more experienced developers here to see if
my approach for doing "convenience" initializations is viable or
potentially problematic.
Since there may be lots of variations on initialization, I reverse
the pattern and have convenience initializers all call a common -init
- (id) init
{
self = [super init];
// init ivars and whatnot....
return self;
}
Convenience initializers for properties of the class invoke this
common -init, and then make adjustments:
- (id) initWithProperty:(PropertyClass*)propertyValue
{
if (self = [self init]) {
[self setProperty:propertyValue];
}
return self;
}
Of course, my invoking code could achieve the same ends with two
lines of code:
SomeClass *instance = [[SomeClass aloc] init];
[instance setProperty:propertyValue];
As I said, my pattern is just a convenience:
SomeClass *instance = [[SomeClass aloc]
initWithProperty:propertyValue];
So, I guess I have two questions here:
1) Is the convenience approach obfuscating (i.e., are the two lines
of code clearer to others who come in to work on the source)?
2) is my way of handling these convenience initializations viable?
Thanks.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden