[sbe-discussion] Re: page 94 / getter setter confusion .. again
Oscar Nierstrasz
oscar at iam.unibe.ch
Wed Oct 17 20:38:34 MEST 2007
Haha! Very cute!
"." is the statement separator in Smalltalk.
What you wrote can be rewritten as:
test := TestClass new.
test.
thing := 'thingy'.
So you have only defined a variable in your Workspace called "thing",
but have not touched your TestClass instance.
Nice puzzler.
oscar
On Oct 17, 2007, at 20:33, Has van der Krieken wrote:
> Hi all,
>
> Suppose I have defined a class TestClass.
>
> Object subclass: #TestClass
> instanceVariableNames: 'thing'
> classVariableNames: ''
> poolDictionaries: ''
> category: 'Test'
>
> Then I can access the instance variable thing by using a dot.
>
> test := TestClass new.
> test.thing := 'thingy'.
>
> When inspect/print test.thing, I get 'thingy'.
> Is the use of the dot standard Smalltalk or a Squeak feature?
>
> Regards, Has.
>
>
> On 10/17/07, Oscar Nierstrasz <oscar at iam.unibe.ch> wrote:
>>
>> Hi Barry,
>>
>> With an object inspector you can access all the "private" instance
>> variables of your object.
>>
>> This suggests that the inspector has a "back door" to access private
>> state.
>>
>> There are quite a few reflective features of Smalltalk, and we are
>> planning to add a chapter on this for the next edition.
>>
>> To access the value variable of the counter, use the method
>> Object>>#instVarNamed:
>> To modify it, you can use Object>>#instVarNamed:put:
>>
>>
>> x := Counter new.
>> x increment.
>> x value --> 1
>> x instVarNamed: 'value' --> 1
>> x instVarNamed: 'value' put: 10.
>> x value --> 10
>>
>>
>> But these method should be avoided for ordinary programs. They are
>> absolutely essential for implementing Inspectors and Debuggers, but
>> you don't want to break encapsulation of ordinary objects in ordinary
>> programs.
>>
>> Oscar
>>
>> On Oct 17, 2007, at 19:45, Barry Davis wrote:
>>
>>> Thanks Damien,
>>>
>>> I understand the sentence in the book now almost!
>>>
>>> "Of course providing setter and getter methods for all the instance
>>> variables of an object does is not good object oriented style."
>>>
>>> a) I was wondering in wich case a getter method would not be ok? I
>>> understand now a setter method could be "dangerous"
>>>
>>> b) I was wondering how " you can always access your instance
>>> variable from your object using its name. " i searched half the
>>> internet how to do this, but i don't know what to type in my
>>> workspace to make this work. How could i for example direct access
>>> the value of SimpleCounter?
>>>
>>>
>>> Thanks in avance for learning me this important concept,
>>>
>>> Much appreciated!!
>>>
>>> Barry
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> You can always
>>>>> access your instance variable from your object using its name.
>>>
>>>
>>>
>>>
>>>
>>>
>>>> Date: Wed, 17 Oct 2007 12:48:18 +0200
>>>> From: damien.cassou at gmail.com
>>>> To: sbe-discussion at iam.unibe.ch
>>>> Subject: [sbe-discussion] Re: page 94 / getter setter
>>> confusion .. again
>>>>
>>>> Hi Barry,
>>>>
>>>> in SimpleCounter, you have an instance variable named value and a
>>> read
>>>> accessor (getter) named #value. There are only two ways of
>>>> modifying
>>>> the value from the outside: using #increase or #decrease. From
>>>> outside, you can't assign 3 to the counter for example. You have to
>>>> use #increase 3 times.
>>>>
>>>> I SimpleCounter had provided a #value: method to modify the
>>>> value of
>>>> the counter directly, it wouldn't had been a counter anymore;
>>> this is
>>>> because adding the method #value: let any user put any value
>>> directly
>>>> in the counter. Then, the counter is not a counter anymore.
>>>>
>>>> Is that clearer?
>>>>
>>>> 2007/10/16, Barry Davis <barry_davis10 at hotmail.com>:
>>>>>
>>>>> Thanks Damien, for your explanation
>>>>>
>>>>> I now understand Instance variables are used to store data and
>>> attributes
>>>>> of instances. They cannot be accessed directly from
>>> ***outside*** an object.
>>>>> Instead, an access method must be used [for outside use].
>>>>>
>>>>> To fully understand this concept i'm now looking for a little
>>> example wich
>>>>> explains both concepts to me, so i can see it.
>>>>>
>>>>> Can you explain this concept with the SimpleCounter example, like
>>>>> http://www.iam.unibe.ch/~ducasse/Videos/SqueakH-264Mov/120-
>>> TestingSimpleCounter.mov
>>>>>
>>>>> Perhaps this example could be added to the book because i
>>> understand this
>>>>> is quite important?
>>>>>
>>>>> Thanks,
>>>>>
>>>>> Barry
>>>>>
>>>>>
>>>>>
>>>>>> Date: Tue, 16 Oct 2007 06:57:04 +0200
>>>>>> From: damien.cassou at gmail.com
>>>>>> To: sbe-discussion at iam.unibe.ch
>>>>>> Subject: [sbe-discussion] Re: page 94 / getter setter
>>> confusion .. again
>>>>>
>>>>>>
>>>>>> Hi Barry,
>>>>>>
>>>>>> in fact, using getter and setter is the only way to access the
>>>>>> instance variables of your object from another object. You
>>> can always
>>>>>> access your instance variable from your object using its name.
>>>>>>
>>>>>> What is bad object-oriented style, is to always provide
>>> access to your
>>>>>> instance variables for the outside. The object should not
>>> reveal its
>>>>>> internal details. For example, if you have a class PhoneBook
>>> which
>>>>>> represents a collection of persons, you should not give
>>> access to the
>>>>>> internal collection. If you do it, you give the user the
>>> possibility
>>>>>> to bypass your class and to modify the collection directly;
>>> this may
>>>>>> be a problem if your PhoneBook caches data, update an underlying
>>>>>> database...
>>>>>>
>>>>>> Is that clearer?
>>>>>>
>>>>>> 2007/10/15, Barry Davis <barry_davis10 at hotmail.com>:
>>>>>>>
>>>>>>> hi i have a question about a line on page 94:
>>>>>>>
>>>>>>> "Of course providing setter and getter methods for all the
>>> instance
>>>>>>> variables of an object does is not good object oriented
>>> style."
>>>>>>>
>>>>>>> i am quite confused by this line, because i do this and
>>> can't find
>>>>>>> information how to get/set a instance variable without a
>>> method"
>>>>>>>
>>>>>>> till know i follow the next rule:
>>>>>>> "always access instance variables using two methods only, a
>>> get and set
>>>>>>> pair. All other methods that need to access the instance
>>> variable shoud
>>>>> send
>>>>>>> one of thes two messages to self."
>>>>>>>
>>>>>>> but according to page 94 (and also page 84) this is bad
>>> object oriented
>>>>>>> style.
>>>>>>>
>>>>>>> question can anybody explain what is the appropriate way /
>>> rule how
>>>>>>> somebody should set/get instance variables. (without
>>> methods??)
>>>>>>>
>>>>>>> thanks in advance,
>>>>>>>
>>>>>>> Barry
>>>>>>>
>>>>>>> ________________________________
>>>>>>> Pas je zoekresultaten aan op JOUW wensen met Live.nl! Live.nl
>>>>>>> _______________________________________________
>>>>>>> Sbe-discussion mailing list
>>>>>>> Sbe-discussion at iam.unibe.ch
>>>>>>>
>>>>> https://www.iam.unibe.ch/mailman/listinfo/sbe-discussion
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Damien Cassou
>>>>>> _______________________________________________
>>>>>> Sbe-discussion mailing list
>>>>>> Sbe-discussion at iam.unibe.ch
>>>>>>
>>>>> https://www.iam.unibe.ch/mailman/listinfo/sbe-discussion
>>>>>
>>>>>
>>>>> ________________________________
>>>>> Publiceer JOUW leven online met Windows Live Spaces: weblog,
>>> foto, video en
>>>>> muziek! Het is gratis! Het is gratis!
>>>>> _______________________________________________
>>>>> Sbe-discussion mailing list
>>>>> Sbe-discussion at iam.unibe.ch
>>>>> https://www.iam.unibe.ch/mailman/listinfo/sbe-discussion
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> Damien Cassou
>>>> _______________________________________________
>>>> Sbe-discussion mailing list
>>>> Sbe-discussion at iam.unibe.ch
>>>> https://www.iam.unibe.ch/mailman/listinfo/sbe-discussion
>>>
>>>
>>> Windows Live Mail: Nu 2gb aan opslag - dat zijn maar liefst 1000
>>> foto's - en nog steeds gratis! Windows Live Mail
>>> _______________________________________________
>>> Sbe-discussion mailing list
>>> Sbe-discussion at iam.unibe.ch
>>> https://www.iam.unibe.ch/mailman/listinfo/sbe-discussion
>>
>> _______________________________________________
>> Sbe-discussion mailing list
>> Sbe-discussion at iam.unibe.ch
>> https://www.iam.unibe.ch/mailman/listinfo/sbe-discussion
>>
> _______________________________________________
> Sbe-discussion mailing list
> Sbe-discussion at iam.unibe.ch
> https://www.iam.unibe.ch/mailman/listinfo/sbe-discussion
More information about the Sbe-discussion
mailing list