When should I use a separate class to represent an

2019-08-09 11:25发布

I have recently been going through the book "Scala by Example" in which the author creates an abstract class to represent a set of ints "IntSet" with two subclasses (EmptySet and NonEmptySet) as follows:

abstract class Stack[A] { 
  def push(x: A): Stack[A] = new NonEmptyStack[A](x, this)
  def isEmpty: Boolean
  def top: A
  def pop: Stack[A]
}

class EmptyStack[A] extends Stack[A] {
  def isEmpty = true 
  def top = error("EmptyStack.top")
  def pop = error("EmptyStack.pop")
}

class NonEmptyStack[A](elem: A, rest: Stack[A]) extends Stack[A] {
  def isEmpty = false
  def top = elem
  def pop = rest
}

My question is this: How useful is this paradigm of representing an empty container as its own class instead of creating one concrete class to handle both the empty and non-empty cases?

1条回答
beautiful°
2楼-- · 2019-08-09 12:17

Each implementation is simpler and more readable as the is-empty-check does not have to be done in the implementation. This leads to better code metric values (like cyclomatic complexity) too.

Moreover, It makes the implementations slightly faster in general, since the distinction between empty and non-empty does not have to be done at runtime. As far as I know, Scala's Set applies this technique and implements different types of sets (used depending on their size) to optimize performance.

Obviously this works for immutable data structures only.

查看更多
登录 后发表回答