Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > When To Use Categories and When to Subclass?

When To Use Categories and When to Subclass?
Thread Tools
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jan 29, 2003, 02:39 AM
 
I'm new to Objective-C and trying to evaluate the merits of categories from a design perspective. Categories appear to be a way to add methods to an exisiting class without altering it's code. That's cool but why would I want to do that? Comming from a Java background it seems more straightforward to just subclass? What am I missing here?

     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Jan 29, 2003, 05:25 AM
 
It sort of depends on how you want to use it once you've made it. If you want to use a subclass you've created, you'll have to be responsible for allocating instances of it. But there are times when you'd like to extend a class where you typically aren't allocating the instances you want to be able to use your new methods on. For those times, use a category -- it adds methods to every instance of the class, not just the ones you allocate yourself.

For example, in our open-source OmniFoundation framework, we define about twenty additional methods for NSArray. Many of them are conveniences that let you perform common tasks in one method call instead of writing the same ten lines of code over and over (like -reversedArray or -randomObject). If these methods were implemented in a subclass, we'd have to create an instance of that subclass any time we wanted to use them -- since a bunch of Apple APIs return NSArrays, it'd be a pain to do [[OFArray alloc] initWithArray:anArray] every time we wanted to use one of our extension methods on one.

Also, in some cases you might want to extend the behavior of objects where it's impossible to allocate them yourself. For example, in OmniAppKit we extend NSColor to provide a property list representation of itself -- this lets us easily save color references in NSUserDefaults for any color the user can pick in the color panel. If we wanted to do that with an NSColor subclass, we'd also have to sublcass NSColorPanel and/or all the pickers inside it (most of which are private Apple classes) to provide instances of our subclass instead of NSColors.
Rick Roe
icons.cx | weblog
     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jan 29, 2003, 09:22 AM
 
Thanks Rickster. Your comments make perfect sense. Once again, I can sleep peacefully!



     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:07 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2