scala 关于面向对象分解的几个问题

icnyk63a  于 2023-03-08  发布在  Scala
关注(0)|答案(1)|浏览(151)

我正在Coursera上学习scala,我看不懂下面这张幻灯片中关于面向对象分解

的局限性
您能帮我简化一下吗?这里说的是什么?如果这不是问这个问题的正确方法,请指导我如何更好地回答。但是,这就是全部。我不理解这张幻灯片。希望有人能帮助我理解它

qfe3c7zg

qfe3c7zg1#

我猜他们的意思是,这个规则可以很容易地在FP中通过使用嵌套模式的模式匹配来实现

sealed trait Expression
case class Lit(value: Int) extends Expression
case class Add(expr1: Expression, expr2: Expression) extends Expression
case class Mult(expr1: Expression, expr2: Expression) extends Expression

def simplify(expr: Expression): Expression = expr match {
  case Add(Mult(a, b), Mult(a1, c)) if a == a1 => Mult(a, Add(b, c))
  case _ => expr
}

但是如何以OOP的方式轻松地实现它呢?

sealed trait Expression {
  def simplify: Expression
}
case class Lit(value: Int) extends Expression {
  override def simplify: Expression = ???
}
case class Add(expr1: Expression, expr2: Expression) extends Expression {
  override def simplify: Expression = ???
}
case class Mult(expr1: Expression, expr2: Expression) extends Expression {
  override def simplify: Expression = ???
}

实际上,它可以使用visitor patterndouble/multiple调度,模拟多方法)来实现

sealed trait Expression {
  def simplify: Expression
  def simplifyAdd(that: Expression): Expression
  def simplifyAddLit(lit: Lit): Expression
  def simplifyAddAdd(add: Add): Expression
  def simplifyAddMult(mult: Mult): Expression
}

case class Lit(value: Int) extends Expression {
  override def simplify: Expression = this
  override def simplifyAdd(that: Expression): Expression = that.simplifyAddLit(this)
  override def simplifyAddLit(lit: Lit): Expression    = Add(lit, this)
  override def simplifyAddAdd(add: Add): Expression    = Add(add, this)
  override def simplifyAddMult(mult: Mult): Expression = Add(mult, this)
}

case class Add(expr1: Expression, expr2: Expression) extends Expression {
  override def simplify: Expression = expr1.simplifyAdd(expr2)
  override def simplifyAdd(that: Expression): Expression = that.simplifyAddAdd(this)
  override def simplifyAddLit(lit: Lit): Expression    = Add(lit, this)
  override def simplifyAddAdd(add: Add): Expression    = Add(add, this)
  override def simplifyAddMult(mult: Mult): Expression = Add(mult, this)
}

case class Mult(expr1: Expression, expr2: Expression) extends Expression {
  override def simplify: Expression = this
  override def simplifyAdd(that: Expression): Expression = that.simplifyAddMult(this)
  override def simplifyAddLit(lit: Lit): Expression    = Add(lit, this)
  override def simplifyAddAdd(add: Add): Expression    = Add(add, this)
  override def simplifyAddMult(mult: Mult): Expression =
    if (mult.expr1 == expr1) Mult(expr1, Add(mult.expr2, expr2)) 
    else Add(mult, this)
}

sealed trait Expression {
  def simplify: Expression
  def simplifyAdd(that: Expression): Expression
  def simplifyAddMult(mult: Mult): Expression
}

case class Lit(value: Int) extends Expression {
  override def simplify: Expression = this
  override def simplifyAdd(that: Expression): Expression = Add(this, that)
  override def simplifyAddMult(mult: Mult): Expression   = Add(mult, this)
}

case class Add(expr1: Expression, expr2: Expression) extends Expression {
  override def simplify: Expression = expr1.simplifyAdd(expr2)
  override def simplifyAdd(that: Expression): Expression = Add(this, that)
  override def simplifyAddMult(mult: Mult): Expression   = Add(mult, this)
}

case class Mult(expr1: Expression, expr2: Expression) extends Expression {
  override def simplify: Expression = this
  override def simplifyAdd(that: Expression): Expression = that.simplifyAddMult(this)
  override def simplifyAddMult(mult: Mult): Expression   =
    if (mult.expr1 == expr1) Mult(expr1, Add(mult.expr2, expr2)) 
    else Add(mult, this)
}

如果我们想让simplify递归呢?

相关问题