About the singleton pattern

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

About the singleton pattern

demarey
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.

smime.p7s (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

Uko2
Sometimes I use #new

> On 08 Apr 2015, at 11:31, Christophe Demarey <[hidden email]> 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.


Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

EstebanLM

> On 08 Apr 2015, at 11:37, Yuriy Tymchuk <[hidden email]> wrote:
>
> Sometimes I use #new
that’s horrible!
completely misleading

>
>> On 08 Apr 2015, at 11:31, Christophe Demarey <[hidden email]> 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.
>
>


Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

Peter Uhnak
In reply to this post by Uko2
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 <[hidden email]> wrote:
Sometimes I use #new

> On 08 Apr 2015, at 11:31, Christophe Demarey <[hidden email]> 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.



Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

EstebanLM
In reply to this post by demarey
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 <[hidden email]> 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.


Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

Peter Uhnak
In reply to this post by EstebanLM
On Wed, Apr 8, 2015 at 11:52 AM, Esteban Lorenzano <[hidden email]> wrote:

> On 08 Apr 2015, at 11:37, Yuriy Tymchuk <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

NorbertHartl
In reply to this post by EstebanLM

> Am 08.04.2015 um 12:00 schrieb Esteban Lorenzano <[hidden email]>:
>
> 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 <[hidden email]> 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.
>
>


Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

Pharo Smalltalk Developers mailing list
In reply to this post by demarey
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 <[hidden email]>
To: Pharo Development List <[hidden email]>
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.

Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

EstebanLM
In reply to this post by Peter Uhnak

On 08 Apr 2015, at 12:01, Peter Uhnák <[hidden email]> wrote:

On Wed, Apr 8, 2015 at 11:52 AM, Esteban Lorenzano <[hidden email]> wrote:

> On 08 Apr 2015, at 11:37, Yuriy Tymchuk <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

demarey
In reply to this post by EstebanLM

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 <[hidden email]> 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.
>
>


smime.p7s (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

Peter Uhnak
In reply to this post by EstebanLM
> 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
Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

Esteban A. Maringolo
In reply to this post by EstebanLM
2015-04-08 7:21 GMT-03:00 Esteban Lorenzano <[hidden email]>:

>
> On 08 Apr 2015, at 12:01, Peter Uhnák <[hidden email]> wrote:
>
> On Wed, Apr 8, 2015 at 11:52 AM, Esteban Lorenzano <[hidden email]>
> wrote:
>>
>>
>> > On 08 Apr 2015, at 11:37, Yuriy Tymchuk <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

Dmitri Zagidulin
In reply to this post by EstebanLM
+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 <[hidden email]> wrote:

On 08 Apr 2015, at 12:01, Peter Uhnák <[hidden email]> wrote:

On Wed, Apr 8, 2015 at 11:52 AM, Esteban Lorenzano <[hidden email]> wrote:

> On 08 Apr 2015, at 11:37, Yuriy Tymchuk <[hidden email]> 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


Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

EstebanLM

On 09 Apr 2015, at 20:32, Dmitri Zagidulin <[hidden email]> 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 <[hidden email]> wrote:

On 08 Apr 2015, at 12:01, Peter Uhnák <[hidden email]> wrote:

On Wed, Apr 8, 2015 at 11:52 AM, Esteban Lorenzano <[hidden email]> wrote:

> On 08 Apr 2015, at 11:37, Yuriy Tymchuk <[hidden email]> 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



Reply | Threaded
Open this post in threaded view
|

Re: About the singleton pattern

Dmitri Zagidulin
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 <[hidden email]> wrote:

On 09 Apr 2015, at 20:32, Dmitri Zagidulin <[hidden email]> 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 <[hidden email]> wrote:

On 08 Apr 2015, at 12:01, Peter Uhnák <[hidden email]> wrote:

On Wed, Apr 8, 2015 at 11:52 AM, Esteban Lorenzano <[hidden email]> wrote:

> On 08 Apr 2015, at 11:37, Yuriy Tymchuk <[hidden email]> 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