Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!maytag!vlsi!fchan From: fchan@vlsi.waterloo.edu (Francis Chan) Newsgroups: comp.lang.smalltalk Subject: Re: Data-Hiding? Really?? Message-ID: <259@vlsi.waterloo.edu> Date: 17 Jan 90 15:48:17 GMT References: <4811d5e9.20b6d@apollo.HP.COM> Reply-To: fchan@vlsi.waterloo.edu (Francis Chan) Organization: U. of Waterloo, Ontario Lines: 59 In article <4811d5e9.20b6d@apollo.HP.COM> yon@apollo.HP.COM (David Yon) writes: > > apple := Apple new. > apple setRipeness: 6. > basket := AppleBasket new. > basket putApple: apple. > anotherApple := basket getApple. > anotherApple setRipeness: 5 > basket inspect. > >--- > You'll find that by setting the ripeness factor of the >anotherApple variable, it changed the state of the object in the >AppleBasket. So the ripeness of the Apple in the AppleBasket will >have been changed to five. On the surface, this appears to violate >the rule that only objects can change their own state. > > Any thoughts on this? > >David Yon I don't think its a problem. The anotherApple happens to be the "same" apple as the apple with ripeness of 6. You got that apple from the basket and (with a wave of the proverbial magic mouse) regressed its ripeness to 5 and then put it back into the basket (that's because the getApple method does not remove it from the basket). In order to get "another" apple, the getApple method should give you a copy of the apple of ripeness 6 i.e. getApple "Answer the Apple in the Basket" ^theApple copy ---- Anyway the apple example probably does not show what you were trying to point to out, I think. Yes, encapsulation does work ... but the setRipeness: method allows direct access from the outside to the instance variable (howRipe). Thus the onus is on the designer of the object to make sure that what needs to be hidden remains so. For the apple example, you could take out direct access to instance variable by removing the setRipeness: method and adding the following methods: "Apple class method" new ^super new initialize "Apple methods" initialize howRipe := 1 "or whatever.." ripen howRipe := howRipe + 1 This way you can only ripen the apple or get the apple of the same ripeness (which you can then ripen). Francis Chan