Does Groovy have support for something like Ruby Modules?

171 views Asked by At

Ruby modules make things like passing a database connection or other dependencies to various objects much easier while allowing for separation of concerns. Does Groovy support a similar functionality? And if so what is it called?

1

There are 1 answers

2
kdabir On BEST ANSWER

In ruby modules are used either as mixins or to namespace a class (e.g. Net::HTTP).

To mixin the behavior you can use @mixin annotation. like examples here http://groovy.codehaus.org/Category+and+Mixin+transformations.

To namespace, groovy uses same mechanism as java i.e. using packages (e.g. groovy.sql.Sql).

I am not sure if that answered your question or not. But for dependency injection, while its common to do it mixin way in ruby (or even in scala/play), I have not seen it done a lot using @mixin in groovy. Usually a DI container like spring is used.