Hi Miguel,<br> A simple rule you can use is:<br><br> Use a class variable if your Singleton will not have subclasses.<br> 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.<br>
<br> A good practice is to implement a #reset method, very useful when cleaning or shutting down images for a release.<br><br>Best regards<br><br>Hernán<br><br><br><div class="gmail_quote">2009/8/4 Miguel Enrique Cobá Martinez <span dir="ltr"><<a href="mailto:miguel.coba@gmail.com">miguel.coba@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">What is the difference between a class variable like Current in Locale<br>
class:<br>
<br>
Object subclass: #Locale<br>
instanceVariableNames: 'id shortDate longDate time decimalSymbol<br>
digitGrouping currencySymbol currencyNotation measurement<br>
offsetLocalToUTC offsetVMToUTC dstActive'<br>
classVariableNames: 'Current CurrentPlatform KnownLocales<br>
LanguageSymbols LocaleChangeListeners PlatformEncodings'<br>
poolDictionaries: ''<br>
category: 'System-Localization'<br>
<br>
that is accessed as (in class side):<br>
<br>
current<br>
"Current := nil"<br>
Current ifNil: [<br>
Current := self determineCurrentLocale.<br>
"Transcript show: 'Current locale: ' , Current localeID asString;<br>
cr"].<br>
^Current<br>
<br>
and current in SmalltalkImage class side:<br>
<br>
SmalltalkImage class<br>
instanceVariableNames: 'current'<br>
<br>
that is accessed as:<br>
current<br>
"Note that this could be implemented differently to avoid the test"<br>
<br>
current isNil<br>
ifTrue: [current := self basicNew].<br>
^ current<br>
<br>
I'm trying to create a singleton but I have some doubts in the correct<br>
implementation.<br>
<br>
I have also seen the explanation on<br>
<br>
<a href="http://coweb.cc.gatech.edu/cs2340/3872" target="_blank">http://coweb.cc.gatech.edu/cs2340/3872</a><br>
<br>
also:<br>
<br>
<a href="http://stackoverflow.com/questions/438729/smalltalk-singleton-pattern-how-do-i-initialize-the-instance-variables" target="_blank">http://stackoverflow.com/questions/438729/smalltalk-singleton-pattern-how-do-i-initialize-the-instance-variables</a><br>
<br>
that corresponds to the first type, using (I suppose) a class variable<br>
and<br>
<br>
<a href="http://wiki.squeak.org/squeak/939" target="_blank">http://wiki.squeak.org/squeak/939</a><br>
<br>
that corresponds to the second type.<br>
<br>
Are there guidelines or pros/cons about singletons on Smalltalk?<br>
<br>
Thanks,<br>
Miguel Cobá<br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@lists.squeakfoundation.org">Beginners@lists.squeakfoundation.org</a><br>
<a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners" target="_blank">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br>