[Newbies] class variable vs class instance variable uses
Hernán Morales Durand
hernan.morales at gmail.com
Tue Aug 4 16:29:30 UTC 2009
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
A good practice is to implement a #reset method, very useful when cleaning
or shutting down images for a release.
2009/8/4 Miguel Enrique Cobá Martinez <miguel.coba at gmail.com>
> What is the difference between a class variable like Current in Locale
> 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 := nil"
> Current ifNil: [
> Current := self determineCurrentLocale.
> "Transcript show: 'Current locale: ' , Current localeID
> and current in SmalltalkImage class side:
> SmalltalkImage class
> instanceVariableNames: 'current'
> that is accessed as:
> "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
> I have also seen the explanation on
> that corresponds to the first type, using (I suppose) a class variable
> that corresponds to the second type.
> Are there guidelines or pros/cons about singletons on Smalltalk?
> Miguel Cobá
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners