Skip to content

Instantly share code, notes, and snippets.

@v6ak
Last active December 19, 2015 21:39
Show Gist options
  • Select an option

  • Save v6ak/6022072 to your computer and use it in GitHub Desktop.

Select an option

Save v6ak/6022072 to your computer and use it in GitHub Desktop.
trait ICalculatingStrategy {
def calculate(a: Int, b: Int): Int
}
object AdderStrategy extends ICalculatingStrategy{
override def calculate(a: Int, b: Int) = a + b
}
object MultiplierStrategy extends ICalculatingStrategy{
override def calculate(a: Int, b: Int) = a * b
}
object CalculatingStrategyFactory {
@inline val StrategyAdd = 2
@inline val StrategyMultiply = 3
private val Strategies = Map(
StrategyAdd -> AdderStrategy,
StrategyMultiply -> MultiplierStrategy
)
def create(strategy: Int) = Strategies(strategy)
}
CalculatingStrategyFactory.create(CalculatingStrategyFactory.StrategyMultiply).calculate(3, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment