Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

groovy singleton pattern

Tags:

groovy

Q1. What is the best way to implement the singleton pattern using groovy? What are the other options available in groovy to support the singleton mechanism?

Any example that would be useful.

Q2. Does groovy support something like File changed listener?

like image 909
anish Avatar asked Sep 30 '11 15:09

anish


1 Answers

Q1

You can make any class a singleton simply by adding a @Singleton annotation (since groovy v1.7.0 at least):

@Singleton
class MyClass {

}

You can then access the singleton instance with

MyClass singleton = MyClass.instance

Q2

I think you're asking if Groovy provides a listener which is called every time a file is changed? I am not aware of any such facility in Groovy. If such a class exists you're more likely to find a Java implementation (which you could use in your Groovy program).

like image 87
Dónal Avatar answered Sep 30 '22 23:09

Dónal