The Traits would be cool to standardise the behaviour and to mark it as such (implicit documentation)
, however, Traits cannot add instance/class variables IIUC, that limits what they can do (and would make it more than one step).
> On 19 Jan 2017, at 11:33, Cyril Ferlicot D. <cyril.ferlicot@gmail.com> wrote:
>
> On 19/01/2017 11:23, Siemen Baader wrote:
>> Is there a trait to quickly create singletons in Pharo, along the way of
>> the Ruby Singleton mixin?
>>
>> https://ruby-doc.org/stdlib-1.9.3/libdoc/singleton/rdoc/ Singleton.html
>>
>> (and ideally also observer/observable, which I am going to need next..)
>>
>> -- Siemen
>
> Hi!
>
> Singleton in Smalltalk are really easy so there is no Trait.
>
> You just need a class variable #UniqueInstance and those methods:
>
> current
>�� �� "Can also be named #default or #instance"
>�� �� �� ��^ UniqueInstance
>�� �� �� �� �� �� �� ��ifNil: [ UniqueInstance := self basicNew; initialize; yourself ]
>
> new
>�� �� �� ��^ self error: 'I am a Singleton. Use current to get my instance or
> reset my instance with a new project. Maybe explain why there is a
> Singleton because we should avoid them if possible.'
>
>
> Now for the observer pattern you have the Announcement framework in Pharo.
>
> I don't really have the time to explain but I think there is some doc on
> internet and on the help browser of Pharo. I found this:
> http://pharo.gemtalksystems.com/book/LanguageAndLibraries/ announcements/
> (I did not read it so I don't know what it's worth)
>
> --
> Cyril Ferlicot
>
> http://www.synectique.eu
>
> 2 rue Jacques Pr��vert 01,
> 59650 Villeneuve d'ascq France
>