前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )

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

作者头像
韩曙亮
发布2023-03-30 11:03:58
1520
发布2023-03-30 11:03:58
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、批量方法委托


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

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

在 StudentManager 中实现

代码语言:javascript
复制
def delegate(Class... classes)

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

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

代码语言:javascript
复制
        def objects = classes.collect{
            // 通过反射创建要委托的对象
            it.newInstance()
        }

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

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

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

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

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

代码语言:javascript
复制
                if (object) {
                    // 注入 name 方法
                    sm.metaClass."$name" = {
                        object.invokeMethod(name, it)
                    }
                    // 调用刚注入的方法
                    invokeMethod(name, args)
                }

方法委托实现如下 :

代码语言:javascript
复制
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)
                }
        }
    }
}

二、完整代码示例


完整代码示例 :

代码语言:javascript
复制
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()

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

执行结果 :

代码语言:javascript
复制
hello1
hello2
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-02-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、批量方法委托
  • 二、完整代码示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档