【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 正常方法调用 | 方法委托实现 | 代码示例 )

简介: 【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 正常方法调用 | 方法委托实现 | 代码示例 )

一、方法委托


1、正常方法调用


定义两个类 , 分别在类中定义不同的方法 ;


class Student1{
    def hello1(){
        println "hello1"
    }
}
class Student2{
    def hello2(){
        println "hello2"
    }
}


再定义一个管理类 , 持有上述类对象作为成员变量 ;

class StudentManager{
    def student1 = new Student1()
    def student2 = new Student2()
}


在 Groovy 脚本中 , 创建该管理类 , 并可以通过该管理类调用不同成员变量的方法 ; 使用该方式调用 , 需要经过两个调用节点 ;


def sm = new StudentManager()

// 使用该方式调用 , 需要经过两个调用节点;
sm.student1.hello1()
sm.student2.hello2()


2、方法委托实现


在 StudentManager 对象中, 如果通过该对象 , 调用 Student1 中的方法 hello1 , 而发现该对象没有该 hello1 方法 , 那么将方法委托给 Student1 对象执行 ;



方法委托实现 : 为 StudentManager 对象注入方法 ,


如果当前调用的是 hello1 方法 , 则执行 student1 的 hello1 方法 ;

如果当前调用的是 hello2 方法 , 则执行 student2 的 hello2 方法 ;

上述操作可以在 def methodMissing(String name, def args) 方法中进行处理 ;


进入到 methodMissing 方法之后 , 先判断成员对象中是否包含指定的方法 ,


if (student1.respondsTo(name, args)) {}

如果成员对象中包含指定方法 , 则向 StudentManager 中注入对应方法 , 在注入的方法闭包中调用成员对象的指定方法 ;


   


StudentManager sm = this
        if (student1.respondsTo(name, args)) {
            // 判断 student1 中是否实现了相应方法
            sm.metaClass."$name" = {
                // 其中 it 是传入的闭包参数 , 也可以写成 args
                student1.invokeMethod(name, it)
            }
            "$name"(args)
        }


方法委托实现如下 :



class StudentManager{
    def student1 = new Student1()
    def student2 = new Student2()
    def methodMissing(String name, def args) {
        /*
            在该 StudentManager 对象中, 如果通过该对象,
            调用 Student1 中的方法 hello1 , 而发现该对象没有该 hello1 方法
            那么将方法委托给 Student1 对象执行
            方法委托 : 为 StudentManager 对象注入方法
                      注入方法执行内容 :
                        如果当前调用的是 hello1 方法 , 则执行 student1 的 hello1 方法
                        如果当前调用的是 hello2 方法 , 则执行 student2 的 hello2 方法
         */
        // 注入方法需要获取 org.codehaus.groovy.runtime.HandleMetaClass 对象进行注入
        StudentManager sm = this
        if (student1.respondsTo(name, args)) {
            // 判断 student1 中是否实现了相应方法
            sm.metaClass."$name" = {
                // 其中 it 是传入的闭包参数 , 也可以写成 args
                student1.invokeMethod(name, it)
            }
            "$name"(args)
        }else if (student2.respondsTo(name, args)) {
            // 判断 student1 中是否实现了相应方法
            sm.metaClass."$name" = {
                // 其中 it 是传入的闭包参数 , 也可以写成 args
                student2.invokeMethod(name, it)
            }
            "$name"(args)
        }
        return null
    }
}


二、完整代码示例


完整代码示例 :

class Student1{
    def hello1(){
        println "hello1"
    }
}
class Student2{
    def hello2(){
        println "hello2"
    }
}
class StudentManager{
    def student1 = new Student1()
    def student2 = new Student2()
    def methodMissing(String name, def args) {
        /*
            在该 StudentManager 对象中, 如果通过该对象,
            调用 Student1 中的方法 hello1 , 而发现该对象没有该 hello1 方法
            那么将方法委托给 Student1 对象执行
            方法委托 : 为 StudentManager 对象注入方法
                      注入方法执行内容 :
                        如果当前调用的是 hello1 方法 , 则执行 student1 的 hello1 方法
                        如果当前调用的是 hello2 方法 , 则执行 student2 的 hello2 方法
         */
        // 注入方法需要获取 org.codehaus.groovy.runtime.HandleMetaClass 对象进行注入
        StudentManager sm = this
        if (student1.respondsTo(name, args)) {
            // 判断 student1 中是否实现了相应方法
            sm.metaClass."$name" = {
                // 其中 it 是传入的闭包参数 , 也可以写成 args
                student1.invokeMethod(name, it)
            }
            "$name"(args)
        }else if (student2.respondsTo(name, args)) {
            // 判断 student1 中是否实现了相应方法
            sm.metaClass."$name" = {
                // 其中 it 是传入的闭包参数 , 也可以写成 args
                student2.invokeMethod(name, it)
            }
            "$name"(args)
        }
        return null
    }
}
def sm = new StudentManager()
// 使用该方式调用 , 需要经过两个调用节点;
sm.student1.hello1()
sm.student2.hello2()
// 方法委托, 直接通过 StudentManager 对象调用 Student1 中的方法
sm.hello1()
// 方法委托, 直接通过 StudentManager 对象调用 Student2 中的方法
sm.hello2()
/*
    方法委托 : 如果调用的某个对象方法没有定义该对象 , 则可以将该方法委托给内部对象执行
 */


执行结果 :

hello1
hello2
hello1
hello2

image.png

目录
相关文章
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 正常方法调用 | 方法委托实现 | 代码示例 )
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 正常方法调用 | 方法委托实现 | 代码示例 )
84 0
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 正常方法调用 | 方法委托实现 | 代码示例 )
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )
127 0
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 使用 @Delegate 注解进行方法委托 )
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 使用 @Delegate 注解进行方法委托 )
128 0
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 使用 @Delegate 注解进行方法委托 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 实现 GroovyInterceptable 接口 | 重写 invokeMethod 方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 实现 GroovyInterceptable 接口 | 重写 invokeMethod 方法 )
145 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 实现 GroovyInterceptable 接口 | 重写 invokeMethod 方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 同时注入普通方法、静态方法、构造方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 同时注入普通方法、静态方法、构造方法 )
113 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 同时注入普通方法、静态方法、构造方法 )
|
Java
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法拦截 JDK 中已经定义的函数 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法拦截 JDK 中已经定义的函数 )
117 0
【Groovy】MOP 元对象协议与元编程 ( Groovy 类内部和外部分别获取 metaClass | 分析获取 metaClass 操作的字节码 | HandleMetaClass 注入方法 )
【Groovy】MOP 元对象协议与元编程 ( Groovy 类内部和外部分别获取 metaClass | 分析获取 metaClass 操作的字节码 | HandleMetaClass 注入方法 )
166 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 通过 MetaClass#invokeMethod 方法调用类其它方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 通过 MetaClass#invokeMethod 方法调用类其它方法 )
141 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 通过 MetaClass#invokeMethod 方法调用类其它方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入普通方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入普通方法 )
117 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入普通方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法实现函数拦截 | 实现函数调用转发 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法实现函数拦截 | 实现函数调用转发 )
126 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法实现函数拦截 | 实现函数调用转发 )