I am looking for a way to initialize a singleton
object with arguments
for configuration. I tried this, it has the risk for re-instantiation.
class A(in:Int) {
def method = {}
}
object A {
var singleton: Option[A] = None
def getInstance(): A = {
if(singleton.isDefined)
singleton.get
else {
throw InitializationException("Object not configured")
}
}
def getInstance(in:Int): A = {
singleton = Some(new A(in))
singleton.get
}
}
Is there a better way?
Aucun commentaire:
Enregistrer un commentaire