I have a class MyFilter which has many different versions. Depending on the input version, I want to invoke that object and apply that filter. Sample is shown below:
object v1{
def apply(l: List[String]) = {
l.filter(_.contains("v1"))
}
}
object v2{
def apply(l: List[String]) = {
l.filter(_.contains("v2"))
}
}
object MyFilter {
def apply(version: String) = (l: List[String]) => {
println("Applying Filter")
version match {
case "v1" => v1(l)
case "v2" => v2(l)
case _ =>
}
}
}
val df = List("a-v1", "b-v1", "c-v2", "d-v2")
println(MyFilter("v1")(df))
println(MyFilter("v2")(df))
Now, If I have many versions, there will be a long list of case in match. How can I avoid it and directly call apply method of Object using passed version which is string. It would be great, if someone can help.
Aucun commentaire:
Enregistrer un commentaire