Re: static vs non-static. Recommendation needed.
Re: static vs non-static. Recommendation needed.
- Subject: Re: static vs non-static. Recommendation needed.
- From: Quincey Morris <email@hidden>
- Date: Mon, 13 Apr 2009 02:23:12 -0700
On Apr 13, 2009, at 01:13, Тимофей Даньшин wrote:
As i don't need that DatabaseCreator to exist for a long time i
thought it better to make it completely static. Sorry if the
terminology is wrong, but what i mean is: it cannot be initialized,
it has no instance variables, it has no instance methods, all it has
is one public class method: + (sqlite3 *)
createDbaseWithDatabaseInfo: (DatabaseInfo *) dbInfo;
However, that proves not very convenient, as i need to break up the
logic of that class into individual steps and I end up passing a lot
of things from one method to another.
Is it all right to init an object just to dealloc it in the next
line (or create an autorelease object using a convenience method for
that matter)? I mean, if i made it non-static, i would have
something like this in the class that uses it:
DatabaseCreator *dbc = [DatabaseCreator creatorWithDBInfo: dbInfo];
sqlite3* database = [dbc database]; // and dbc would no longer be
used.
Or is it better to put up with the inconveniences of having that
thing static?
You can have the best of both worlds. Your DatabaseCreator class can
have a *public* interface with just a single class method:
+ (sqlite3 *) createDbaseWithDatabaseInfo: (DatabaseInfo *) dbInfo;
but that doesn't prevent the class method from *privately* creating an
object and disposing of it before returning:
+ (sqlite3 *) createDbaseWithDatabaseInfo: (DatabaseInfo *) dbInfo {
DatabaseCreator *dbc = [DatabaseCreator creatorWithDBInfo: dbInfo];
return [dbc database]; // and dbc would no longer be used.
}
Important: If you're *not* using garbage collection, make sure you
retain the database object appropriately and release dbc appropriately
before returning.
_______________________________________________
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