Last active
December 19, 2015 21:39
-
-
Save v6ak/6022072 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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