Re: Sensible way to extend base class?
Re: Sensible way to extend base class?
- Subject: Re: Sensible way to extend base class?
- From: Ken Thomases <email@hidden>
- Date: Wed, 20 May 2009 08:01:50 -0500
On May 20, 2009, at 7:09 AM, Graham Cox wrote:
I have a base class in a framework, let's call it A. This has a
number of subclasses, B C and D.
I have some functionality I'd like to add that would apply to all A,
B, C and D. So a category on A would be fine - except that I need
some support in the form of a couple of ivars. The added
functionality isn't appropriate to the framework, so though I have
the source for A, adding ivars that have no place there grieves me.
It also grieves me that the solution appears to be separate
subclasses of B, C and D to add the functionality which must be
duplicated for each class.
Any solution I've missed?
It's possible for a category to provide ivar-like functionality. You
can maintain a "category variable" in the same manner as one
implements a "class variable" in Objective-C: a file-scope static in
the implementation file. That variable could be a mapping from A
instance pointers to a value (or dictionary of values).
It's non-trivial, though. You have to take care of thread safety in
maintaining the mapping.
The other trick is knowing when the A instance has been deallocated so
that you can remove the entry from the mapping. You can do this using
method swizzling on -[A dealloc].
It's all a gross hack, but it can be done.
Regards,
Ken
_______________________________________________
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