Re: Newbie question: how to release an object that has been autoreleased
Re: Newbie question: how to release an object that has been autoreleased
- Subject: Re: Newbie question: how to release an object that has been autoreleased
- From: James Bucanek <email@hidden>
- Date: Wed, 22 Sep 2004 12:43:03 -0700
Glenn Andreas wrote on Wednesday, September 22, 2004:
>This isn't all that different than the way a class cluster is
>designed, and works because "init" can return something other than
>"self". Thus there isn't a need to have a factory method to decide
>what to do - this logic can be pushed into the init method itself.
<clip>
>By doing this, you avoid the "newBufferNotAutoreleased" method - if
>you don't want to create an autoreleased object, [[Buffer alloc]
>init] will work just fine.
Except for the one important detail which I pointed out in my earlier post: All of the details which Buffer would need to initialize itself are @private or @protected instance variables in BufferPool.
Having the BufferPool produce Buffer objects was a matter of encapsulation. For Buffer to initialize itself will require me to expose all (or at least some) of the private data of BufferPool to the world. At the very least, I'd have to pass BufferPool to Buffer ([[Buffer alloc] initWithPool:pool]).
>Never overlook the ability to return something other than self from
>init - its a very powerful technique (you can also use this to
>guarentee that there is only one instance of a given class, for
>example, which makes it easy to have a singleton that the user can
>"override").
I agree, and it's one of Obj-C's most elegant features (in my opinion). Essentially, the factory pattern that is employed in the Java world is built-in to the Obj-C constructor.
But sometimes a class just doesn't have access to the data it needs to make that decision....
James
--
James Bucanek <mailto:email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden