[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
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 <miguel.coba at gmail.com>
> 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
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20090804/04c788ef/attachment.htm
More information about the Beginners
mailing list