Re: Objective-C Question
Re: Objective-C Question
- Subject: Re: Objective-C Question
- From: Steve Ehrenfried <email@hidden>
- Date: Mon, 22 Sep 2003 02:56:48 -0700 (PDT)
--- Marcel Weiher <email@hidden> wrote:
>
> You can't write a Category that deals with more
>
than one class, even,
>
> or with anything but *one* *class*.
>
>
Sure you can.
>
Please explain.
I'm a newbie to Obj C, but my understanding is that
categories are just way to add one or more methods to
an existing class. For instance, you could add a
method called "firstLetter" to the NSString class to
return just the first letter of a string, and
everywhere NSString is used in your app, this method
automatically becomes available (you just need to
invoke the method(s); you don't have to refer to the
name of the category since you didn't subclass
NSString).
However, if you want to add methods to multiple
classes, you would have to implement a category for
each class. This could become onerous. FWIW, here's a
quote from a "Cocoa Programming for Mac OS X" by
Hillegass re categories:
"Warning: If you use a lot of categories, your code
may become difficult for others to read and maintain.
Some large teams discourage the use of categories for
this reason."
Perhaps you are thinking of protocols, which are
analogous to interfaces in Java. Multiple classes can
implement a specific protocol; thus a protocol can
span multiple classes.
From what I can tell categories are mostly a hack for
adding methods to classes that were developed by
someone else. Good OOP should focus on classes and
protocols, IMHO. Still this is a cool feature that can
be a Godsend when needed.
From what I can tell Obj C is missing several things
which a good OOP language like Java has:
1) Being a able to make a method and/or class "final",
i.e. it can't be overridden or subclassed.
2) Being to able to have a partial implementation of a
class, i.e. an "abstract class". (Perhaps Obj C
supports this but I just don't know about it. If there
is, how do you specify that instances of a specific
class can't be instantiated?)
3) Built in support for exceptions, though this is
more of a convenience than part of the object model.
Great implementation in Java, horrendous
implementation in C++ (the function signature doesn't
specify what exceptions are thrown, so how do you know
[unless it's documented, which it probably isn't] what
you need to catch or re-throw? It's real easy for an
uncaught exception to percolate up the stack).
4) Being able to specify the access control of members
(public, protected, private). Isn't everything public
in Obj C? Sorry, I'm a newbie here, but isn't data
encapsulation part of the object model?
5) No namespaces (C++) or packages (Java). Again, Java
has a much cleaner implementation and is better
thought through, IMHO. Anything like this in Obj C?
Thanks,
Steve
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.