Groovy探索之MOP 一 invokeMethod和methodMissing方法

来源:互联网 发布:淘宝虚拟产品货源 编辑:程序博客网 时间:2024/05/21 09:34

           Groovy探索之MOP invokeMethodmethodMissing方法

 

 

终于要谈到Groovy语言的MOP特性了,我在前面的章节中零星的谈到了它,却始终没有系统的来谈到它。这是因为Groovy语言的MOP特性实在是太灵活了,比如本章节要谈到的“invokeMethod”和“methodMissing”方法,它们的功能有很大的相似之处,而区别却相当的微妙。但是,不管怎么样,Groovy语言的MOP编程都是我们必须掌握的。而这个系列我没有计划多少个部分谈完,跟《Groovy探索之闭包》系列一样,探索一部分说一部分。

 

本节要谈到的“invokeMethod”方法,我们在《Groovy探索》系列中已经有一个章节谈到过,本节还要谈到,主要是要谈谈它和“methodMissing”的区别。

对于“invokeMethod”方法,大家一定很熟悉了,我们可以用下面一个简单的例子来看看它的作用:

 

class InvokeTestor1 {

   

    def hello()

    {

       'invoke hello directly'

    }

   

    def invokeMethod(String name,Object args)

    {

       return "unknown method $name(${args.join(',')})"

    }

 

  static void main(args) {

     

      def it = new InvokeTestor1()

     

      println it.hello()

     

      println it.foo("mark",19)

   

  }

 

}

 

 

运行的结果为:

 

invoke hello directly

unknown method foo(mark,19)

 

 

可以看出,对于一个对象的方法调用来说,如果这个方法能够被分派出去,如上面的“hello”方法,可以在InvokeTestor1类中找到,就被分派给InvokeTestor1类的“hello”方法;如果不能被分派,如上面的“foo”方法,则调用“invokeMethod”方法。

Groovy语言中,还有一个方法也可以实现上面的功能,这就是“methodMissing”方法,请看下面的例子:

 

class MethodTestor1 {

   

    def hello()

    {

       "invoke hello directly"

    }

   

    def methodMissing(String name,args)

    {

       return "unknown method $name(${args.join(',')})"

    }

 

  static void main(args) {

     

      def mt = new MethodTestor1()

     

      println mt.hello()

     

      println mt.foo('mark',19)

   

  }

 

}

 

 

我们还是来看看上面的代码的运行结果:

 

invoke hello directly

unknown method foo(mark,19)

 

 

可以看到,“methodMissing”方法就像它的名字一样,如果方法可以在类中找得到,那么就调用该方法;如果找不到,那么就是“missing method”,就可以调用“methodMissing”方法了。跟“invokeMethod”功能非常类似。

这点大家都清楚,但实际上,invokeMethod”在Groovy语言中是用来分派一个对象的所有方法的。要做到这一点,就需要借助于“GroovyInterceptable”接口。请看下面的例子:

 

 

class InvokeTestor2 implements GroovyInterceptable{

   

    def hello()

    {

       "invoke hello directly"

    }

   

    def invokeMethod(String name,Object args)

    {

       return "unknown method $name(${args.join(',')})"

    }

 

  static void main(args) {

     

      def it = new InvokeTestor2()

     

      println it.hello()

     

      println it.foo('mark',19)

   

  }

 

}

 

 

运行结果为:

 

unknown method hello()

unknown method foo(mark,19)

 

 

从运行结果可以看出,“invokeMethod”方法的确可以分派所有的方法,只要我们实现“GroovyInterceptable”接口即可。

而“methodMissing”方法呢,即使类实现了“GroovyInterceptable”接口,它也不能使用“methodMissing”方法来分派所有的方法。请看下面的例子:

 

 

class MethodTestor2 implements GroovyInterceptable{

   

    def hello()

    {

       "invoke hello directly"

    }

   

    def methodMissing(String name,args)

    {

       return "unknown method $name(${args.join(',')})"

    }

 

  static void main(args) {

     

      def mt = new MethodTestor2()

     

      println mt.hello()

     

      println mt.foo('mark',19)

     

   

  }

 

}

 

 

它的运行结果为:

 

invoke hello directly

unknown method foo(mark,19)

 

 

通过了上面的比较,我们可以看出“invokeMethod”方法和“methodMissing”方法的微妙区别:即,“invokeMethod”方法可以分派所有的方法,包括一个类已经实现了的和未实现的方法;而它实现上面的功能是通过这个类实现“GroovyInterceptable”接口达到的。而“methodMissing”方法则只能分派一个类未实现的方法,无论它是否实现了“GroovyInterceptable”接口。

 

这种区别的确很微妙,如果我们想让一个方法来管理一个类所有方法的调用,那么我们必须使用“invokeMethod”方法;如果我们只想通过一个方法来管理一个类的所有“missing method”,即不能被分派出去的方法,那么使用“methodMissing”方法是比较有效的;当然,“invokeMethod”方法也能实现“methodMissing”方法的功能。

 

 

原创粉丝点击