Hi, We use quite often the singleton pattern but when I need to use one, I always need to ask myself "What is the selector to get this singleton?". We use either aClass>>current, aClass>>default or aClass>>uniqueInstance. Could we agree on the selector to use and update existing code? To get a quick overview, I searched about these methods in a Pharo4 image and get these results: ((SystemNavigation default allImplementorsOf: #default) select: #isMetaSide) size. "45" ((SystemNavigation default allImplementorsOf: #current) select: #isMetaSide) size. "40" ((SystemNavigation default allImplementorsOf: #uniqueInstance) select: #isMetaSide) size. "19" Your opinion? Christophe.
Sometimes I use #new
On 08 Apr 2015, at 11:31, Christophe Demarey <Christophe.Demarey@inria.fr> wrote:
Hi,
We use quite often the singleton pattern but when I need to use one, I always need to ask myself "What is the selector to get this singleton?". We use either aClass>>current, aClass>>default or aClass>>uniqueInstance. Could we agree on the selector to use and update existing code?
To get a quick overview, I searched about these methods in a Pharo4 image and get these results: ((SystemNavigation default allImplementorsOf: #default) select: #isMetaSide) size. "45" ((SystemNavigation default allImplementorsOf: #current) select: #isMetaSide) size. "40" ((SystemNavigation default allImplementorsOf: #uniqueInstance) select: #isMetaSide) size. "19"
Your opinion?
Christophe.
On 08 Apr 2015, at 11:37, Yuriy Tymchuk <yuriy.tymchuk@me.com> wrote:
Sometimes I use #new thatâs horrible! completely misleading
On 08 Apr 2015, at 11:31, Christophe Demarey <Christophe.Demarey@inria.fr> wrote:
Hi,
We use quite often the singleton pattern but when I need to use one, I always need to ask myself "What is the selector to get this singleton?". We use either aClass>>current, aClass>>default or aClass>>uniqueInstance. Could we agree on the selector to use and update existing code?
To get a quick overview, I searched about these methods in a Pharo4 image and get these results: ((SystemNavigation default allImplementorsOf: #default) select: #isMetaSide) size. "45" ((SystemNavigation default allImplementorsOf: #current) select: #isMetaSide) size. "40" ((SystemNavigation default allImplementorsOf: #uniqueInstance) select: #isMetaSide) size. "19"
Your opinion?
Christophe.
On Wed, Apr 8, 2015 at 11:52 AM, Esteban Lorenzano <estebanlm@gmail.com> wrote:
On 08 Apr 2015, at 11:37, Yuriy Tymchuk <yuriy.tymchuk@me.com> wrote:
Sometimes I use #new thatâs horrible! completely misleading
I'm curious, if it is singleton, should you worry from outside about the fact that it is a singleton? Shouldn't that be hidden from the user? Peter
On 08 Apr 2015, at 12:01, Peter Uhnák <i.uhnak@gmail.com> wrote:
On Wed, Apr 8, 2015 at 11:52 AM, Esteban Lorenzano <estebanlm@gmail.com <mailto:estebanlm@gmail.com>> wrote:
On 08 Apr 2015, at 11:37, Yuriy Tymchuk <yuriy.tymchuk@me.com <mailto:yuriy.tymchuk@me.com>> wrote:
Sometimes I use #new thatâs horrible! completely misleading
I'm curious, if it is singleton, should you worry from outside about the fact that it is a singleton? Shouldn't that be hidden from the user? nope. new means new. you are confusing your user if you send a new message and you receive an already existing (aka not new) object. the literature existing always recommend to use an specific method (usually #instance or #uniqueInstance, in smalltalk.. who decided to use #uniqueInstance as a convention).
Esteban
Peter
Sometimes I use #new thatâs horrible! completely misleading
I'm curious, if it is singleton, should you worry from outside about the fact that it is a singleton? Shouldn't that be hidden from the user?
nope. new means new. you are confusing your user if you send a new message and you receive an already existing (aka not new) object. the literature existing always recommend to use an specific method (usually #instance or #uniqueInstance, in smalltalk.. who decided to use #uniqueInstance as a convention).
Thanks for the explanation; I'm learning every day. :) Peter
2015-04-08 7:21 GMT-03:00 Esteban Lorenzano <estebanlm@gmail.com>:
On 08 Apr 2015, at 12:01, Peter Uhnák <i.uhnak@gmail.com> wrote:
On Wed, Apr 8, 2015 at 11:52 AM, Esteban Lorenzano <estebanlm@gmail.com> wrote:
On 08 Apr 2015, at 11:37, Yuriy Tymchuk <yuriy.tymchuk@me.com> wrote:
Sometimes I use #new thatâs horrible! completely misleading
I'm curious, if it is singleton, should you worry from outside about the fact that it is a singleton? Shouldn't that be hidden from the user?
nope. new means new. you are confusing your user if you send a new message and you receive an already existing (aka not new) object. the literature existing always recommend to use an specific method (usually #instance or #uniqueInstance, in smalltalk.. who decided to use #uniqueInstance as a convention).
+1 I like #current as the selector, because most of the times I use singleton as a globally accessible object. And as with any global reference it is good practice to avoid them as much as possible. Regards! Esteban A. Maringolo
+1 to standardizing #uniqueInstance. Also, would it be appropriate to mark intended singleton creation methods with a pragma? (So that they could be flagged as special in the UI, etc). On Wed, Apr 8, 2015 at 6:21 AM, Esteban Lorenzano <estebanlm@gmail.com> wrote:
On 08 Apr 2015, at 12:01, Peter Uhnák <i.uhnak@gmail.com> wrote:
On Wed, Apr 8, 2015 at 11:52 AM, Esteban Lorenzano <estebanlm@gmail.com> wrote:
On 08 Apr 2015, at 11:37, Yuriy Tymchuk <yuriy.tymchuk@me.com> wrote:
Sometimes I use #new thatâs horrible! completely misleading
I'm curious, if it is singleton, should you worry from outside about the fact that it is a singleton? Shouldn't that be hidden from the user?
nope. new means new. you are confusing your user if you send a new message and you receive an already existing (aka not new) object. the literature existing always recommend to use an specific method (usually #instance or #uniqueInstance, in smalltalk.. who decided to use #uniqueInstance as a convention).
Esteban
Peter
On 09 Apr 2015, at 20:32, Dmitri Zagidulin <dmitri@zagidulin.net> wrote:
+1 to standardizing #uniqueInstance. but as I said⦠not always.
Also, would it be appropriate to mark intended singleton creation methods with a pragma? (So that they could be flagged as special in the UI, etc).
no, we should not abuse the use of pragmas. tools can recognise appropriate selectors (like it happens with initialisation, etc.) without need to pollute the code. Esteban
On Wed, Apr 8, 2015 at 6:21 AM, Esteban Lorenzano <estebanlm@gmail.com <mailto:estebanlm@gmail.com>> wrote:
On 08 Apr 2015, at 12:01, Peter Uhnák <i.uhnak@gmail.com <mailto:i.uhnak@gmail.com>> wrote:
On Wed, Apr 8, 2015 at 11:52 AM, Esteban Lorenzano <estebanlm@gmail.com <mailto:estebanlm@gmail.com>> wrote:
On 08 Apr 2015, at 11:37, Yuriy Tymchuk <yuriy.tymchuk@me.com <mailto:yuriy.tymchuk@me.com>> wrote:
Sometimes I use #new thatâs horrible! completely misleading
I'm curious, if it is singleton, should you worry from outside about the fact that it is a singleton? Shouldn't that be hidden from the user?
nope. new means new. you are confusing your user if you send a new message and you receive an already existing (aka not new) object. the literature existing always recommend to use an specific method (usually #instance or #uniqueInstance, in smalltalk.. who decided to use #uniqueInstance as a convention).
Esteban
Peter
What are pragmas for, ideally? I've only seen them mentioned in the context of flagging menu items. (And I know some of the frameworks like Seaside use them to mark http GET / POST etc handlers). On Thu, Apr 9, 2015 at 2:34 PM, Esteban Lorenzano <estebanlm@gmail.com> wrote:
On 09 Apr 2015, at 20:32, Dmitri Zagidulin <dmitri@zagidulin.net> wrote:
+1 to standardizing #uniqueInstance.
but as I said⦠not always.
Also, would it be appropriate to mark intended singleton creation methods with a pragma? (So that they could be flagged as special in the UI, etc).
no, we should not abuse the use of pragmas. tools can recognise appropriate selectors (like it happens with initialisation, etc.) without need to pollute the code.
Esteban
On Wed, Apr 8, 2015 at 6:21 AM, Esteban Lorenzano <estebanlm@gmail.com> wrote:
On 08 Apr 2015, at 12:01, Peter Uhnák <i.uhnak@gmail.com> wrote:
On Wed, Apr 8, 2015 at 11:52 AM, Esteban Lorenzano <estebanlm@gmail.com> wrote:
On 08 Apr 2015, at 11:37, Yuriy Tymchuk <yuriy.tymchuk@me.com> wrote:
Sometimes I use #new thatâs horrible! completely misleading
I'm curious, if it is singleton, should you worry from outside about the fact that it is a singleton? Shouldn't that be hidden from the user?
nope. new means new. you are confusing your user if you send a new message and you receive an already existing (aka not new) object. the literature existing always recommend to use an specific method (usually #instance or #uniqueInstance, in smalltalk.. who decided to use #uniqueInstance as a convention).
Esteban
Peter
Also ((SystemNavigation default allImplementorsOf: #instance) select: #isMetaSide) size. "10" ((SystemNavigation default allImplementorsOf: #singleton) select: #isMetaSide) size. "1" This reminded me of this https://twitter.com/ID_AA_Carmack/status/575788622554628096 considering we already use a/an for parameters, this can be quite fitting. But some consensus would be nice⦠personally I don't really like that I have to think about the fact that it is a singleton, so The* + #new sounds interesting. Otherwise to me #current sounds much more fitting than #default (in fact I would expect them return different things if #current: has been used). Peter On Wed, Apr 8, 2015 at 11:37 AM, Yuriy Tymchuk <yuriy.tymchuk@me.com> wrote:
Sometimes I use #new
On 08 Apr 2015, at 11:31, Christophe Demarey < Christophe.Demarey@inria.fr> wrote:
Hi,
We use quite often the singleton pattern but when I need to use one, I always need to ask myself "What is the selector to get this singleton?". We use either aClass>>current, aClass>>default or aClass>>uniqueInstance. Could we agree on the selector to use and update existing code?
To get a quick overview, I searched about these methods in a Pharo4 image and get these results: ((SystemNavigation default allImplementorsOf: #default) select: #isMetaSide) size. "45" ((SystemNavigation default allImplementorsOf: #current) select: #isMetaSide) size. "40" ((SystemNavigation default allImplementorsOf: #uniqueInstance) select: #isMetaSide) size. "19"
Your opinion?
Christophe.
I disagree⦠probably they are not well used, but this is the meaning of each selector: - #uniqueInstance. As it says⦠a pure singleton. example: Author class>>#uniqueInstance. Ideally, classes using this method also cancels #new. - #default. It gives you a default instance (as name says), but you can also create instances of it, for other uses. example: RPackageOrganizer class>>#default. - #current. It gives you a singleton, but something that can change under certain circumstances, like a UI theme or a platform change. example: OSPlatform class>>#current. so⦠I do agree there are a lot of wrong uses there, that needs to be fixed⦠but each of the used selectors have a meaning, and a different meaning than the others. cheers, Esteban
On 08 Apr 2015, at 11:31, Christophe Demarey <Christophe.Demarey@inria.fr> wrote:
Hi,
We use quite often the singleton pattern but when I need to use one, I always need to ask myself "What is the selector to get this singleton?". We use either aClass>>current, aClass>>default or aClass>>uniqueInstance. Could we agree on the selector to use and update existing code?
To get a quick overview, I searched about these methods in a Pharo4 image and get these results: ((SystemNavigation default allImplementorsOf: #default) select: #isMetaSide) size. "45" ((SystemNavigation default allImplementorsOf: #current) select: #isMetaSide) size. "40" ((SystemNavigation default allImplementorsOf: #uniqueInstance) select: #isMetaSide) size. "19"
Your opinion?
Christophe.
Am 08.04.2015 um 12:00 schrieb Esteban Lorenzano <estebanlm@gmail.com>:
I disagree⦠probably they are not well used, but this is the meaning of each selector:
- #uniqueInstance. As it says⦠a pure singleton. example: Author class>>#uniqueInstance. Ideally, classes using this method also cancels #new.
- #default. It gives you a default instance (as name says), but you can also create instances of it, for other uses. example: RPackageOrganizer class>>#default.
- #current. It gives you a singleton, but something that can change under certain circumstances, like a UI theme or a platform change. example: OSPlatform class>>#current.
so⦠I do agree there are a lot of wrong uses there, that needs to be fixed⦠but each of the used selectors have a meaning, and a different meaning than the others.
+1 Norbert
On 08 Apr 2015, at 11:31, Christophe Demarey <Christophe.Demarey@inria.fr> wrote:
Hi,
We use quite often the singleton pattern but when I need to use one, I always need to ask myself "What is the selector to get this singleton?". We use either aClass>>current, aClass>>default or aClass>>uniqueInstance. Could we agree on the selector to use and update existing code?
To get a quick overview, I searched about these methods in a Pharo4 image and get these results: ((SystemNavigation default allImplementorsOf: #default) select: #isMetaSide) size. "45" ((SystemNavigation default allImplementorsOf: #current) select: #isMetaSide) size. "40" ((SystemNavigation default allImplementorsOf: #uniqueInstance) select: #isMetaSide) size. "19"
Your opinion?
Christophe.
Le 8 avr. 2015 à 12:00, Esteban Lorenzano a écrit :
I disagree⦠probably they are not well used, but this is the meaning of each selector:
- #uniqueInstance. As it says⦠a pure singleton. example: Author class>>#uniqueInstance. Ideally, classes using this method also cancels #new.
- #default. It gives you a default instance (as name says), but you can also create instances of it, for other uses. example: RPackageOrganizer class>>#default.
- #current. It gives you a singleton, but something that can change under certain circumstances, like a UI theme or a platform change. example: OSPlatform class>>#current.
You're right default does not have the same meaning. You have a default instance but you can create new ones. For #uniqueInstance and #current, the difference is subtle. You warn users that the singleton you get may change regarding the environment.
so⦠I do agree there are a lot of wrong uses there, that needs to be fixed⦠but each of the used selectors have a meaning, and a different meaning than the others.
Yes, I did not get all the different meanings mostly because the right selector is not always used in the good context. On the other side, I wonder about the #uniqueInstance selector. Are there a lot of situations where you absolutely do not want another instance of this class (even for test purposes)?
cheers, Esteban
On 08 Apr 2015, at 11:31, Christophe Demarey <Christophe.Demarey@inria.fr> wrote:
Hi,
We use quite often the singleton pattern but when I need to use one, I always need to ask myself "What is the selector to get this singleton?". We use either aClass>>current, aClass>>default or aClass>>uniqueInstance. Could we agree on the selector to use and update existing code?
To get a quick overview, I searched about these methods in a Pharo4 image and get these results: ((SystemNavigation default allImplementorsOf: #default) select: #isMetaSide) size. "45" ((SystemNavigation default allImplementorsOf: #current) select: #isMetaSide) size. "40" ((SystemNavigation default allImplementorsOf: #uniqueInstance) select: #isMetaSide) size. "19"
Your opinion?
Christophe.
We should also consider the cases of globals like Processor and Transcript.  ----------------- Benoît St-Jean Yahoo! Messenger: bstjean Twitter: @BenLeChialeux Pinterest: benoitstjean IRC: lamneth Blogue: endormitoire.wordpress.com "A standpoint is an intellectual horizon of radius zero". (A. Einstein) From: Christophe Demarey <Christophe.Demarey@inria.fr> To: Pharo Development List <pharo-dev@lists.pharo.org> Sent: Wednesday, April 8, 2015 5:31 AM Subject: [Pharo-dev] About the singleton pattern Hi, We use quite often the singleton pattern but when I need to use one, I always need to ask myself "What is the selector to get this singleton?". We use either aClass>>current, aClass>>default or aClass>>uniqueInstance. Could we agree on the selector to use and update existing code? To get a quick overview, I searched about these methods in a Pharo4 image and get these results: ((SystemNavigation default allImplementorsOf: #default) select: #isMetaSide) size. "45" ((SystemNavigation default allImplementorsOf: #current) select: #isMetaSide) size. "40" ((SystemNavigation default allImplementorsOf: #uniqueInstance) select: #isMetaSide) size. "19" Your opinion? Christophe.
participants (8)
-
Benoit St-Jean -
Christophe Demarey -
Dmitri Zagidulin -
Esteban A. Maringolo -
Esteban Lorenzano -
Norbert Hartl -
Peter Uhnák -
Yuriy Tymchuk