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.