I have a Scala App that has a trait
that implements some function(s) and a class
which extends that trait
.
The class mentioned above also has a function which calls the function that is defined in the parent trait using it's parameter.
I observed this in Spark + Kafka implementation using Scala. I'm guessing this is some kind of design pattern but I don't know which one. Is it Cake Pattern? Dependency Injection Pattern? Or something else?
Below is the code I'm referring to:
trait SparkApplication {
def sparkConfig: Map[String, String]
def withSparkContext(f: SparkContext => Unit): Unit = {
val conf = new SparkConf()
sparkConfig.foreach { case (k, v) => conf.setIfMissing(k, v) }
val sc = new SparkContext(conf)
f(sc)
}
}
trait SparkStreamingApplication extends SparkApplication {
def withSparkStreamingContext(f: (SparkContext, StreamingContext) => Unit): Unit = {
withSparkContext { sc =>
val ssc = new StreamingContext(sc, Seconds(streamingBatchDuration.toSeconds))
ssc.checkpoint(streamingCheckpointDir)
f(sc, ssc)
ssc.start()
ssc.awaitTermination()
}
}
}