Created
January 13, 2012 03:06
-
-
Save omnisis/1604372 to your computer and use it in GitHub Desktop.
An example of using interfaces with flexible closures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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