zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )

方法批量对象编程协议 Groovy 委托 MOP
2023-06-13 09:18:00 时间

文章目录

一、批量方法委托


在上一篇博客 【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 正常方法调用 | 方法委托实现 | 代码示例 ) 中 , 将 StudentManager 对象的方法委托给了其内部的 student1 和 student2 成员 , 在 methodMissing 方法中进行方法委托 , 需要使用 student.respondsTo(name, args) 代码 , 逐个判断调用的方法是否在 student1 或 student2 成员中 ; 如果 StudentManager 中定义了很多成员 , 那么就需要逐个进行判定 , 写起来很繁琐 ;

下面介绍一种实现方法委托的方式 , 可以优雅的处理上述问题 ;

在 StudentManager 中实现

def delegate(Class... classes)

方法 , 方法接收可变长度的 Class 对象作为参数 ;

首先 , 通过反射 , 创建委托对象 ;

        def objects = classes.collect{
            // 通过反射创建要委托的对象
            it.newInstance()
        }

然后 , 通过 HandleMetaClass 注入 methodMissing 方法 ;

        // 注入方法需要获取 org.codehaus.groovy.runtime.HandleMetaClass 对象进行注入
        StudentManager sm = this
        sm.metaClass.methodMissing = {
            String name, def args ->
		}

再后 , 在 objects 数组中查找哪个对象中包含 name(args) 方法 , 返回该对象赋值给 object 对象 ; 该步骤确保被代理类中有指定的方法 ;

                // 在 objects 数组中查找哪个对象中包含 name(args) 方法
                // 返回该对象赋值给 object 对象
                def object = objects.find{
                    it.respondsTo(name, args)
                }

最后 , 如果最终找到的 object 方法不为空 , 则向 StudentManager 中注入相应方法 , 然后调用该注入的方法 ;

                if (object) {
                    // 注入 name 方法
                    sm.metaClass."$name" = {
                        object.invokeMethod(name, it)
                    }
                    // 调用刚注入的方法
                    invokeMethod(name, args)
                }

方法委托实现如下 :

class StudentManager{
    {
        // 代码块中调用 delegate 方法 , 传入要委托的类
        delegate(Student1, Student2)
    }

    /**
     * 实现方法委托
     * @param classes 可变长度的 Class 对象
     */
    def delegate(Class... classes) {
        def objects = classes.collect{
            // 通过反射创建要委托的对象
            it.newInstance()
        }
        // 注入方法需要获取 org.codehaus.groovy.runtime.HandleMetaClass 对象进行注入
        StudentManager sm = this
        sm.metaClass.methodMissing = {
            String name, def args ->
                // 在 objects 数组中查找哪个对象中包含 name(args) 方法
                // 返回该对象赋值给 object 对象
                def object = objects.find{
                    it.respondsTo(name, args)
                }
                if (object) {
                    // 注入 name 方法
                    sm.metaClass."$name" = {
                        object.invokeMethod(name, it)
                    }
                    // 调用刚注入的方法
                    invokeMethod(name, args)
                }
        }
    }
}

二、完整代码示例


完整代码示例 :

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

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

class StudentManager{
    {
        // 代码块中调用 delegate 方法 , 传入要委托的类
        delegate(Student1, Student2)
    }

    /**
     * 实现方法委托
     * @param classes 可变长度的 Class 对象
     */
    def delegate(Class... classes) {
        def objects = classes.collect{
            // 通过反射创建要委托的对象
            it.newInstance()
        }
        // 注入方法需要获取 org.codehaus.groovy.runtime.HandleMetaClass 对象进行注入
        StudentManager sm = this
        sm.metaClass.methodMissing = {
            String name, def args ->
                // 在 objects 数组中查找哪个对象中包含 name(args) 方法
                // 返回该对象赋值给 object 对象
                def object = objects.find{
                    it.respondsTo(name, args)
                }
                if (object) {
                    // 注入 name 方法
                    sm.metaClass."$name" = {
                        object.invokeMethod(name, it)
                    }
                    // 调用刚注入的方法
                    invokeMethod(name, args)
                }
        }
    }
}

def sm = new StudentManager()

// 方法委托, 直接通过 StudentManager 对象调用 Student1 中的方法
sm.hello1()
// 方法委托, 直接通过 StudentManager 对象调用 Student2 中的方法
sm.hello2()

/*
    方法委托 : 如果调用的某个对象方法没有定义该对象 , 则可以将该方法委托给内部对象执行
 */

执行结果 :

hello1
hello2