A straight forward implementation of a Singleton pattern without using Metaclass:
class A{
static final instance = new A()
private A(){}
static getInstance(){return instance}
}
Why is it that the private constructor is not respected by groovy? Though one can tell by intuition that it is a singleton looking at the pattern, Unlike java, such an implementation does not restrict the programmer from creating a new instance of this class using new A() outside the scope of this class. So shouldn’t this be considered as a flaw in its implementation?
You can replace the code above with:
and access the singleton instance via: