scala:将对象包装在另一个对象中的更简单方法

时间:2012-11-20 13:12:28

标签: scala inheritance playframework-2.0 playframework-2.1

在Play 2.1应用程序中,我有以下代码(它只是一个请求包装器,可以删除任何尾部斜杠):

class NormalizedRequest(request: RequestHeader) extends RequestHeader {

  val headers = request.headers
  val id = request.id
  val method = request.method
  val queryString = request.queryString
  val remoteAddress = request.remoteAddress
  val tags = request.tags
  val version = request.version

  // strip first part of path and uri if it matches http.path config
  val path = if (request.path == "/") "/" else request.path.stripSuffix("/")
  val uri = path + {
    if(request.rawQueryString == "") ""
    else "?" + request.rawQueryString
  }
}

object NormalizedRequest {
  def apply(request: RequestHeader) = new NormalizedRequest(request)
}

这种代码很常见,你只需将一个对象包装在另一个

我想知道是否有更简单的方法来实现它,理想情况下它会像(伪代码灵感来自案例类):

object NormalizedRequest {
  def apply(request: RequestHeader) = {
    val path = if (request.path == "/") "/" else request.path.stripSuffix("/")
    val uri = path + {
      if(request.rawQueryString == "") ""
      else "?" + request.rawQueryString
    }
    request.copy(path = path, uri = uri)
  }
}

1 个答案:

答案 0 :(得分:5)

如果我理解正确,您需要在scala中要求更简洁的Decorator模式版本。你仍然需要你的"包装"与你的内部类相同的类型(通过扩展它或一个公共的基础/特征),以便能够将它传递给一些期望接收内部类的实例(或公共基础/特征)的函数)。

您在伪代码中编写的内容实际上几乎是合法的scala,您只需更改applyNormalizedRequest的定义即可返回扩展RequestHeader的匿名类。

即。而不是

class NormalizedRequest(request: RequestHeader) extends RequestHeader {
    //.... "decorated" logic here
}
object NormalizedRequest {
  def apply(request: RequestHeader) = new NormalizedRequest(request)
}

你会有

object NormalizedRequest {
  def apply(request: RequestHeader) = new RequestHeader {
    // ...
    // instead of having a separate NormalizedRequest class
    // define its behaviour here in anonymous form    
    }
}

简化示例:

// our inner class and companion object 
// (a simplified version of your RequestHeader)
class Inner() {def test="hello"}; object Inner {
    def apply() = new Inner()
} 

// our wrapper
object Outer {
    def apply(inner: Inner) = new Inner {
        override def test=inner.test + "!"
    }   
}

然而,虽然它可以为您节省一些按键,但我认为您的可读性会下降。