Scala工厂方法,用于具有不同隐含体的库



我想支持Json和Parquet文件格式。客户端不应该关心它们的实现,但必须传递一个类型来标识格式。

到目前为止,我有两个类有这样的签名:

class ParquetFileWriter[T](val path: String)(implicit val writer: ParquetWriter[T]) extends FileWriter[T]

class JsonFileWriter[T](val path: String)(implicit writer: JsonWriter[T]) extends FileWriter[T]

他们扩展的特征:

trait FileWriter[T] {
def write(items: Seq[T]): Unit
}

我想创建一个工厂类来通过参数构建类:

class Factory {
def of[T](format: Format): FileWriter[T] = {
format match {
case ParquetSpark =>
new ParquetFileWriter[T](defaultPath)
case Json =>
new JsonFileWriter[T](defaultPath)
}
}
}

问题是ParquetFileWriter和JsonFileWriter需要implicits(当它们从spray.json和com.github.mjakubowski84.parquet4s库到达时,我无法控制它们(。

如果格式依赖于不同的隐含词,我如何实现它们的工厂?我在编译时得到一个"找不到隐式值"。

如果您同时拥有两个implicit,只需添加上下文边界就足够了

class Factory {
def of[T: ParquetWriter : JsonWriter](format: Format): FileWriter[T] = {
format match {
case ParquetSpark =>
new ParquetFileWriter[T]("defaultPath")
case Json =>
new JsonFileWriter[T]("defaultPath")
}
}
}

如果您可能只有这两个隐含项中的一个请尝试使Factory成为类型类

trait Factory[T] {
def of(format: Format): FileWriter[T]
}
trait LowPriorityFactories {
implicit def parquet[T: ParquetWriter]: Factory[T] = {
case ParquetSpark => new ParquetFileWriter[T]("defaultPath")
case _            => throw new Exception
}
implicit def json[T: JsonWriter]: Factory[T] = {
case Json => new JsonFileWriter[T]("defaultPath")
case _    => throw new Exception
}
}
object Factory extends LowPriorityFactories {
def of[T](format: Format)(implicit factory: Factory[T]): FileWriter[T] = factory.of(format)
implicit def jsonParquet[T: JsonWriter : ParquetWriter]: Factory[T] = {
case ParquetSpark => new ParquetFileWriter[T]("defaultPath")
case Json         => new JsonFileWriter[T]("defaultPath")
}
}

现在,如果您同时拥有这两个隐含项,这适用于JsonParquetSpark

implicit val jsonWriter: JsonWriter[Int] = null
implicit val parketWriter: ParquetWriter[Int] = null
Factory.of[Int](Json)
Factory.of[Int](ParquetSpark)

如果你只有隐含的JsonWriter,这只适用于Json

implicit val jsonWriter: JsonWriter[Int] = null
Factory.of[Int](Json)
Factory.of[Int](ParquetSpark) // Exception

如果你只有隐含的ParquetWriter,这只适用于ParquetSpark

implicit val parketWriter: ParquetWriter[Int] = null
Factory.of[Int](Json) // Exception
Factory.of[Int](ParquetSpark)

还有一个选项是使选择更具类型级别将其从运行时转移到编译时(这样就根本不需要模式匹配,编译错误而不是运行时异常(。

sealed trait Format
case object ParquetSpark extends Format
type ParquetSpark = ParquetSpark.type
case object Json extends Format
type Json = Json.type
trait Factory[T, F <: Format] {
def of: FileWriter[T]
}
object Factory {
def of[T, F <: Format](implicit factory: Factory[T, F]): FileWriter[T] = factory.of
implicit def parquet[T: ParquetWriter]: Factory[T, ParquetSpark] = new Factory[T, ParquetSpark] {
override def of: FileWriter[T] = new ParquetFileWriter[T]("defaultPath")
}
implicit def json[T: JsonWriter]: Factory[T, Json] = new Factory[T, Json] {
override def of: FileWriter[T] = new JsonFileWriter[T]("defaultPath")
}
}
{
implicit val jsonWriter: JsonWriter[Int] = null
implicit val parketWriter: ParquetWriter[Int] = null
Factory.of[Int, Json]
Factory.of[Int, ParquetSpark]
}
{
implicit val jsonWriter: JsonWriter[Int] = null
Factory.of[Int, Json]
//  Factory.of[Int, ParquetSpark] // doesn't compile
}
{
implicit val parketWriter: ParquetWriter[Int] = null
//  Factory.of[Int, Json] // doesn't compile
Factory.of[Int, ParquetSpark]
}

相关内容

  • 没有找到相关文章

最新更新