Function Scala是否提供了一种方法;委托书;或;“装饰”;不重复参数列表的函数?

Function Scala是否提供了一种方法;委托书;或;“装饰”;不重复参数列表的函数?,function,scala,parameters,proxy,functional-programming,Function,Scala,Parameters,Proxy,Functional Programming,我有一个带有(公认愚蠢的)名称应用程序的类。 还有一个类ApplicationRepository,它具有私有保存(应用程序)方法 我想向ApplicationRepository添加一个方法,该方法创建一个应用程序实例(完全根据构造函数参数)并保存它。 我不想公开、保存或使用Application类型的参数 def createApplication(firstDay:String, lastDay:String, details:String, substitute:User, projec

我有一个带有(公认愚蠢的)名称应用程序的类。 还有一个类ApplicationRepository,它具有私有保存(应用程序)方法

我想向ApplicationRepository添加一个方法,该方法创建一个应用程序实例(完全根据构造函数参数)并保存它。 我不想公开、保存或使用Application类型的参数

def createApplication(firstDay:String, lastDay:String, details:String, substitute:User, projectManager:User) = {
  val a = Application(firstDay, lastDay, details, substitute, projectManager)
  save(a)
}
基本上,我的问题归结为:


在Scala中-我可以创建一个与“代理”函数具有相同参数列表的“代理”函数,传递所有参数而不关心它们,然后执行不同的操作吗?

我认为不能忽略参数。但您应该尝试为您的类定义伴生对象:

object Application {
  def apply(firstDay:String, lastDay:String, details:String, substitute:User, projectManager:User) = {
    //place for your logic
  }
}

这是在工厂中隐藏构造逻辑的一种非常常见的方法,Scala为您提供了开箱即用的工厂。您也可以尝试使用mixin并在构造函数中动态地保存mixin逻辑,但我认为这将是一个糟糕的设计。

您可以使用宏来实现。这似乎是一个非常罕见的案例,我不会为此烦恼。

两年后,我遇到了这种情况,我使用java代理解决了我的案例。真正的神奇之处在于使用java的()和()

此答案所基于的()的帽子提示

scala> :paste
// Entering paste mode (ctrl-D to finish)

import java.lang.reflect.{Method, InvocationHandler, Proxy}

object ProxyTesting {

  // You need an interface or java's proxy to work.
  trait ProxyTestIface {
    // your function signature here
    def createApplication( firstDay:String, lastDay:String, details:String, substitute:String, projectManager:String):Unit = ???
  }

  // your object implementation
  class ProxyTest extends ProxyTestIface{
    // your function implementation
    override def createApplication( firstDay:String, lastDay:String, details:String, substitute:String, projectManager:String):Unit = {

      println("Actual Function is called")

      Unit
    }
  }

  def test() {
    val obj = new ProxyTest

    val impl = Proxy.newProxyInstance(
      obj.getClass.getClassLoader,
      Array(classOf[ProxyTestIface]),
      new InvocationHandler {
        def invoke(proxy: scala.AnyRef, method: Method, args: Array[AnyRef]): AnyRef = {
          // this shows you can execute prior and post your decorated function.
          println(s"Proxy Funcion is called: ${args.mkString(",")}")
          val res = method.invoke(obj, args:_*)
          println(s"Proxy Funcion is called: 2nd time ${args.mkString(",")}")
          res
        }
      }
    ).asInstanceOf[ProxyTestIface]

    impl.createApplication("firstDayMsg", "lastDayMsg", "detailsMsg", "substituteMsg", "projgMgrMsg")
  }

}

// Exiting paste mode, now interpreting.

import java.lang.reflect.{Method, InvocationHandler, Proxy}
defined object ProxyTesting

scala> ProxyTesting.test()
Proxy Funcion is called: firstDayMsg,lastDayMsg,detailsMsg,substituteMsg,projgMgrMsg
Actual Funcion is called
Proxy Funcion is called: 2nd time firstDayMsg,lastDayMsg,detailsMsg,substituteMsg,projgMgrMsg

scala>

谢谢你的建议。我已经有了一个伴生对象(在我上面的代码中,在应用程序前面没有“新的”…),函数代理是一种罕见的情况?也许在Scala中是:)。