Re: Is encapsulation deprecated in ObjC?
Re: Is encapsulation deprecated in ObjC?
- Subject: Re: Is encapsulation deprecated in ObjC?
- From: Charles Jolley <email@hidden>
- Date: Thu, 16 Aug 2001 13:51:33 -0500
On Thursday, August 16, 2001, at 01:27 PM, Chris Gehlker wrote:
>
This is exactly what I was talking about. I'll pick on Apple since they
>
are
>
big enough to take it. "Learning Cocoa" is full of simplistic setters
>
that
>
are either not used in the sample code provided or used only in
>
messages to
>
self. Furthermore, "Learning Cocoa" shows a technique for hiding
>
methods in
>
the .m file but never applies that technique to those setters when they
>
are
>
only used for messages to self. I started to conclude from that and a
>
few
>
other examples that "It's a Cocoa convention to allow any object to mess
>
with another objects instance variables." I am quite relived to discover
>
that this tentative conclusion was wrong.
BTW, I remember reading some old documentation several years ago stating
that one should create accessor methods for all ivars subclasses might
want to access to help limit the "fragile base class problem." If you
change the implementation of your class in such a way that a given ivar
is no longer used, you can still provide accessors for that ivar that
calculate the value by some other means. Thus, subclasses will "just
work" without having to be modified.
Since these accessors are for subclasses to use, they cannot be
privatethough you could create a separate category that would be
imported only by subclasses. When used in this manner, accessors
actually support encapsulation, not only among unrelated objects but
even among subclasses. I have found this concept to be very useful!
I haven't looked much at the key/value protocol, but it sounds like that
changes the rules on this topic. However, "Learning Cocoa" is just an
update of several old NeXT tutorials, so this may be the historical
reason for including those methods in the example apps.
Cheers,
-Charles