Created
July 29, 2014 21:32
-
-
Save agenovese/82dc31743641f059adf2 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
abstract class Strategy[T, V]() { | |
def doSomething(x: Int): V | |
def unDoSomething(x: V): T | |
} | |
class StrategyOne() extends Strategy[Int, String] { | |
def doSomething(x: Int): String = {"..."} | |
def unDoSomething(x: String): Int = {1} | |
} | |
class StrategyTwo() extends Strategy[Double, List[Int]] { | |
def doSomething(x: Int): List[Int] = {List(1,2,3)} | |
def unDoSomething(x: List[Int]): Double= {4.3} | |
} | |
class Worker[T, V](strategy: Strategy[T, V]) { | |
def run() { | |
val res = strategy.doSomething(5) //res is a T | |
val res2 = strategy.unDoSomething(res) //res2 is a V | |
println(res2) | |
} | |
} | |
val worker1 = new Worker(new StrategyOne()) | |
val worker2 = new Worker(new StrategyTwo()) | |
val strategies = Map("one" -> new StrategyOne(), "two" -> new StrategyTwo()) | |
val worker = new Worker(strategies("one")) | |
worker.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment