Skip to content

数据模型

Song Kun edited this page Jan 3, 2018 · 1 revision

Data Model

将使用 andor 表达的 data model 转化为 Scala 代码。

The Product Type Pattern

第一种模式是:A has a B and C,可以用 case class 或者 trait 对其建模:

case class A(b: B, c: C)

trait A {
  def b: B
  def c: C
}

The Sum Type Pattern

第二种模式为:A is a B or C,使用 sealed trait + final case class 对其建模:

sealed trait A

final case class B() extends A
final case class C() extends A

Algebraic Data Types

代数数据类型是使用以上两种模式(product type + sum type)的任意数据。

Clone this wiki locally