• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [short] Categories vs. subclassing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [short] Categories vs. subclassing


  • Subject: Re: [short] Categories vs. subclassing
  • From: "Erik M. Buck" <email@hidden>
  • Date: Tue, 26 Oct 2004 14:11:12 -0400

Short answer:

Categories add methods to existing classes.
Subclassing creates new classes.

If you use a category to add a method to a framework class, instances of that class returned by a framework will have the method. If you add a method via a subclass, you may have no way to convince the framework to return instances of your subclass instead of whatever class the framework was compiled with.

Cocoa includes several categories on NSObject which effectively add methods to any object you encounter. This practice is sometimes called an informal protocol.

Categories are sometimes used just to break the implementation of a class into multiple files.

Categories are a handy way to extend class clusters which are tricky to subclass.

Categories may be used to preserve the Model/View/Controller partitioning or other desirable partitioning. Parts of a single class can be implement in different partitions/subsystems as appropriate.


For example, NSString is defined in the Foundation framework where there is no knowledge of display issues.

The Application Kit framework contains the NSStringAdditions category that adds display capabilities to every NSString object if and only if the Application Kit framework is linked into your application. http://developer.apple.com/documentation/Cocoa/Reference/ ApplicationKit/ObjC_classic/Classes/NSStringAdditions.html

There is a clear partitioning of functionality and it is possible to use strings without requiring linkage to the Application Kit, but if you do link to the Application Kit, you get the extra features automatically.




_______________________________________________
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
  • Prev by Date: System Idle Time
  • Next by Date: Re: System Idle Time
  • Previous by thread: Re: System Idle Time
  • Next by thread: ORM
  • Index(es):
    • Date
    • Thread