Skip to content

Instantly share code, notes, and snippets.

@Dierk
Created April 6, 2014 19:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dierk/10010410 to your computer and use it in GitHub Desktop.
Save Dierk/10010410 to your computer and use it in GitHub Desktop.
Using modular traits in Groovy 2.3.0-beta-1
trait HasId {
long id
}
trait HasVersion {
long version
}
trait Persistent {
boolean save() { println "saving ${this.dump()}" }
}
trait Entity implements Persistent, HasId, HasVersion {
boolean save() {
version++
Persistent.super.save()
}
}
class Publication implements Entity {
String title
}
class Book extends Publication {
String isbn
}
Entity gina = new Book(id:1, version:1, title:"gina", isbn:"111111")
gina.save()
assert gina.version == 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment