mardi 9 janvier 2018

Which design pattern my Scala application is using?

which scala pattern my app is using when it has a trait with some function implemented. After it has a class that extends the trait. This class also has a function and inside this function it has a block that calls the function of the trait and uses its parameter. I saw this in a Sparck+Kafka implementation using Scala. I guess this is some pattern, but I don't know to say which one it is. If it is Cake Patter, Dependency Injection Pattern, or even another one. Below is the example that I am 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()
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire