I looked through the jsoftware website but couldn't find any references 
tocreating singleton objects in J.So I created my own singleton pattern for J. 
It seems to work.

coclass 'Singleton'

NB. static instanceinstance =: 0

NB. private constructorcreate =. verb defineval =: 1)

NB. Public gettergetinstance =: verb defineif. instance = 0 do. instance =: 
conew 'Singleton'instanceelse. instanceend.)

NB. Verb for checking.say =: verb defineval =: val + 100t =. 'HEY't)

destroy =: codestroy

One thing is, I'm not sure whether making create private stops conew being 
called explicitly.To create THE instance or get it, the user should call 
getinstance_Singleton_ ''
But users can still call conew 'Singleton' from outside the class without an 
error being raised.
                                          
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to