mercredi 6 avril 2016

Scala object onload

I want to implement a factory. There have to be only one instance of this factory in my app. My factory needs to do some preparation before it creates the first MyObject, and only then. Is there any better way to do it than

object MyObjectFactory {
    private var isApplied:Boolean = false
    def apply() = {
        if(!isApplied) {
            isApplied = true;
            onLoad
        }
        //do something
        new MyObject
    }
    def onLoad {
        //prepare environment
    }
}

Aucun commentaire:

Enregistrer un commentaire