• 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: Is encapsulation deprecated in ObjC?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is encapsulation deprecated in ObjC?


  • Subject: Re: Is encapsulation deprecated in ObjC?
  • From: Brian Hill <email@hidden>
  • Date: Thu, 16 Aug 2001 11:58:33 -0500

On Thursday, August 16, 2001, at 11:18 AM, Ondra Cada wrote:

OTOH, in a *VERY REMOTE* sense, encapsulation _IS_ deprecated -- see the
key/value protocol.

But, as for new classes, the idea is quite clean: do implement just those
accessor methods you *DO* need for the abstraction, and no more! *IF* some
code *HAPPENS* to need to violate the encapsulation, well, the key/value
protocol is here.

(Actually, it is questionable whether it does violate encapsulation or not;
I would like not to raise flamewar on it. Do study the thing and decide for
yourself whether you feel it does /go across encapsulation/ or not. What's
important: you don't really need accessor methods, unless they truly are part
of the abstract class API.)

On yet the other hand (assuming we have 3 hands), there is still a bit of control a framework author can exert over what gets accessed via key value coding.

For example, if I have a class with a *really private* instance variable that really should never, ever be accessed outside the instance, you can implement the 'get' and 'set' methods that key value coding will look for and use appropriately.

For example:

- (id)reallyPrivateVariable
{
[NSException raise:NSInvalidArgumentException
format:@"reallyPrivateVariable is private!"];
}
- (void)setReallyPrivateVariable:(id)value
{
[NSException raise:NSInvalidArgumentException
format:@"I said that reallyPrivateVariable was private!"];

}

If someone tries to access the 'reallyPrivateVariable' ivar via key value coding, it should default to using the accessor methods above and throw an exception. I can't honestly think of many reasons why you'd want to go to these lengths to protect a variable, but it's possible to do, anyway.

Brian



email@hidden http://personalpages.tds.net/~brian_hill
___________________________________________________________
"Why? I came into this game for adventure - go anywhere, travel
light, get in, get out, wherever there's trouble, a man alone.
Now they've got the whole country sectioned off and you can't
move without a form. I'm the last of a breed."
-- Archibald "Harry" Tuttle, Rogue HVAC Repairman
___________________________________________________________


References: 
 >Re: Is encapsulation deprecated in ObjC? (From: Ondra Cada <email@hidden>)

  • Prev by Date: Re: Is encapsulation deprecated in ObjC?
  • Next by Date: Re: Is encapsulation deprecated in ObjC?
  • Previous by thread: Re: Is encapsulation deprecated in ObjC?
  • Next by thread: Re: Is encapsulation deprecated in ObjC?
  • Index(es):
    • Date
    • Thread