Skip to content

Instantly share code, notes, and snippets.

@omnisis
Created January 13, 2012 03:06
Show Gist options
  • Save omnisis/1604372 to your computer and use it in GitHub Desktop.
Save omnisis/1604372 to your computer and use it in GitHub Desktop.
An example of using interfaces with flexible closures
class StrategyRunner {
Map strategyMap = [:]
interface IStrategy {
boolean apply(arg)
};
def addStrategy(String name, Closure cl) {
strategyMap[name] = cl as IStrategy
}
def init() {
addStrategy("s1", this.&s1_apply)
addStrategy("s2", { println "strategy2, ignore arg"; false})
}
def s1_apply(arg) {
println "your arg was: ${arg}"
return true
}
def go() {
strategyMap.each {
it.value.apply('arg')
}
}
static void main(args) {
println "in main..."
StrategyRunner runner = new StrategyRunner()
runner.init()
runner.go()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment