I'm not positive of the exact definition of singleton, but there are 2 common 
intentions for it.

1.  Have exactly one instance of an object.

in this case a locale acts as both a singleton, or as class variables.  It does 
not require conew, as the class/singleton is "instanced" on file load.

2. Have 0 or 1 instance of an object.

This version creates a minor problem of requiring a function to get an object 
if it exists.  Its a minor problem because to access members of the object you 
need to either assign the return value to a variable or send string messages to 
the return value.

If your response to it not existing is to create the object, then you are 
better off with the first version.

One legitimate use of this latter pattern is only letting the first creator set 
the settings of the class/instance, and then make them immutable.

There are 2 patterns I like for imposing any control over constructors of an 
object

a.
new_Singleton_ =: 4 : '... (x;y) conew 18!:5'''''

above can also be monadic.  It changes the constructor from params conew 
'Singleton' to new_Singleton_ params, but this allows you to setup any custom 
guards or tracking system, or creation substitution/prevention as part of the 
instancing process.

It does not prevent you from calling conew directly, and "ruining everything", 
so treating the new name in a locale as an almost reserved word can be 
convenient practice to copy and paste consistency among classes.

b.

newSingleton_z_ =: 4 : '... (x;y) conew 18!:5'''' '
getSingleton_z_ =: ...

this provides a one size fits all way of creating a singleton from any 
class/locale.  called as:

newSingleton_Singleton_ params

works because locales default to coinsert z inside them, so when you substitute 
a VERB defined in z calling it in another locale, it sees the called locale as 
the current one, so you can do the logic of checking the current locale for a 
"conventional" name, and then returning it or setting it after a conew call.

you could also do:

newSingleton_javalike_ =: 4 : '... (x;y) conew 18!:5'''' '

coclass 'Singleton'
coinsert 'javalike'

and you still call it as:
newSingleton_Singleton_ params

and define all the methods you want in the javalike locale, and then use them 
in all the classes you want to be javalike by adding the line coinsert 
'javalike' to those locales.


----- Original Message -----
From: Jon Hough <[email protected]>
To: "[email protected]" <[email protected]>
Cc: 
Sent: Wednesday, August 13, 2014 5:25:54 AM
Subject: [Jprogramming] Singleton Objects in J

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

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to