class variable vs class instance variable uses

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

class variable vs class instance variable uses

Miguel Cobá
What is the difference between a class variable like Current in Locale
class:

Object subclass: #Locale
        instanceVariableNames: 'id shortDate longDate time decimalSymbol
digitGrouping currencySymbol currencyNotation measurement
offsetLocalToUTC offsetVMToUTC dstActive'
        classVariableNames: 'Current CurrentPlatform KnownLocales
LanguageSymbols LocaleChangeListeners PlatformEncodings'
        poolDictionaries: ''
        category: 'System-Localization'

that is accessed as (in class side):

current
        "Current := nil"
        Current ifNil: [
                Current := self determineCurrentLocale.
                "Transcript show: 'Current locale: ' , Current localeID asString;
cr"].
        ^Current

and current in SmalltalkImage class side:

SmalltalkImage class
        instanceVariableNames: 'current'

that is accessed as:
current
        "Note that this could be implemented differently to avoid the test"

        current isNil
                ifTrue: [current := self basicNew].
        ^ current

I'm trying to create a singleton but I have some doubts in the correct
implementation.

I have also seen the explanation on

http://coweb.cc.gatech.edu/cs2340/3872

also:

http://stackoverflow.com/questions/438729/smalltalk-singleton-pattern-how-do-i-initialize-the-instance-variables

that corresponds to the first type, using (I suppose) a class variable
and

http://wiki.squeak.org/squeak/939

that corresponds to the second type.

Are there guidelines or pros/cons about singletons on Smalltalk?

Thanks,
Miguel Cobá

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: class variable vs class instance variable uses

hernanmd
Hi Miguel,
  A simple rule you can use is:

  Use a class variable if your Singleton will not have subclasses.
  Use a instance class instance variable if your Singleton will have one or many subclasses, i.e. each class in the hierarchy will need its own Singleton copy.

  A good practice is to implement a #reset method, very useful when cleaning or shutting down images for a release.

Best regards

Hernán


2009/8/4 Miguel Enrique Cobá Martinez <[hidden email]>
What is the difference between a class variable like Current in Locale
class:

Object subclass: #Locale
       instanceVariableNames: 'id shortDate longDate time decimalSymbol
digitGrouping currencySymbol currencyNotation measurement
offsetLocalToUTC offsetVMToUTC dstActive'
       classVariableNames: 'Current CurrentPlatform KnownLocales
LanguageSymbols LocaleChangeListeners PlatformEncodings'
       poolDictionaries: ''
       category: 'System-Localization'

that is accessed as (in class side):

current
       "Current := nil"
       Current ifNil: [
               Current := self determineCurrentLocale.
               "Transcript show: 'Current locale: ' , Current localeID asString;
cr"].
       ^Current

and current in SmalltalkImage class side:

SmalltalkImage class
       instanceVariableNames: 'current'

that is accessed as:
current
       "Note that this could be implemented differently to avoid the test"

       current isNil
               ifTrue: [current := self basicNew].
       ^ current

I'm trying to create a singleton but I have some doubts in the correct
implementation.

I have also seen the explanation on

http://coweb.cc.gatech.edu/cs2340/3872

also:

http://stackoverflow.com/questions/438729/smalltalk-singleton-pattern-how-do-i-initialize-the-instance-variables

that corresponds to the first type, using (I suppose) a class variable
and

http://wiki.squeak.org/squeak/939

that corresponds to the second type.

Are there guidelines or pros/cons about singletons on Smalltalk?

Thanks,
Miguel Cobá

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: class variable vs class instance variable uses

Bert Freudenberg
In reply to this post by Miguel Cobá
On 04.08.2009, at 17:28, Miguel Enrique Cobá Martinez wrote:

> What is the difference between a class variable like Current in  
> Locale class:


A class variable exists once per image. A class instance variable  
exists once per subclass.

If your singleton class has no subclasses, it doesn't matter. If it  
has subclasses, then each subclass will have a different singleton if  
you use a class instance variable. With a class variable, you only  
have one singleton for all subclasses.

- Bert -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: class variable vs class instance variable uses

Miguel Cobá
El mar, 04-08-2009 a las 18:46 +0200, Bert Freudenberg escribió:

> On 04.08.2009, at 17:28, Miguel Enrique Cobá Martinez wrote:
>
> > What is the difference between a class variable like Current in  
> > Locale class:
>
>
> A class variable exists once per image. A class instance variable  
> exists once per subclass.
>
> If your singleton class has no subclasses, it doesn't matter. If it  
> has subclasses, then each subclass will have a different singleton if  
> you use a class instance variable. With a class variable, you only  
> have one singleton for all subclasses.
>

Woa, thanks Bert and Hernán, that was a really clear explanation. Now I
understand the issues here.

Cheers,
Miguel Cobá

> - Bert -
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: class variable vs class instance variable uses

Göran Krampe
Hi!

Or to rephrase it once more:

A class variable is like a global, but only accessible from the class,
its instances and from subclasses and their instances. Kinda like a
"scoped global". A bit similar to a "static" variable in say Java -
"There Can Be Only One" :)

A class instance variable is a much more "natural" thing really. It is
simply an instance variable in the class *itself*. Remember that classes
are real objects in Smalltalk and thus can have instance variables as
well as behavior.

Also, this means we naturally have "inheritance on the class side" which
means that just like class side methods are inherited by subclasses
(unlike static methods in Java which really are just like C functions)
these class instance variables are also inherited by subclasses. But
remember - inheritance doesn't mean that the subclasses will "see" the
variable of the superclass - it means that the subclasses will have
their *own* such instance variable, which again is natural since each
class is a different object.

And also, naturally, class instance variables are not accessible from
the instances of the class!

So class instance variables behave just like regular instance variables
- but *in the class itself*. Btw, if you type a class name, select it,
and press alt-i you get an inspector on the class object and you can see
all its instance variables, as you can see there is a whole bunch.

Yes, this was a much longer explanation but might have cleared up some
question marks still lingering. :)

regards, Göran

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: class variable vs class instance variable uses

Miguel Cobá
El mié, 05-08-2009 a las 09:15 +0200, Göran Krampe escribió:

> Hi!
>
> Or to rephrase it once more:
>
> A class variable is like a global, but only accessible from the class,
> its instances and from subclasses and their instances. Kinda like a
> "scoped global". A bit similar to a "static" variable in say Java -
> "There Can Be Only One" :)
>
> A class instance variable is a much more "natural" thing really. It is
> simply an instance variable in the class *itself*. Remember that classes
> are real objects in Smalltalk and thus can have instance variables as
> well as behavior.
>
> Also, this means we naturally have "inheritance on the class side" which
> means that just like class side methods are inherited by subclasses
> (unlike static methods in Java which really are just like C functions)
> these class instance variables are also inherited by subclasses. But
> remember - inheritance doesn't mean that the subclasses will "see" the
> variable of the superclass - it means that the subclasses will have
> their *own* such instance variable, which again is natural since each
> class is a different object.
>
> And also, naturally, class instance variables are not accessible from
> the instances of the class!
>
> So class instance variables behave just like regular instance variables
> - but *in the class itself*. Btw, if you type a class name, select it,
> and press alt-i you get an inspector on the class object and you can see
> all its instance variables, as you can see there is a whole bunch.
>
> Yes, this was a much longer explanation but might have cleared up some
> question marks still lingering. :)
>
> regards, Göran

Thanks Göran for the alternative explanation. Indeed it fills some gaps
in my understanding and use of those features of squeak.

Miguel Cobá
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners