zl程序教程

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

当前栏目

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

方法对象编程协议代码 实现 示例 调用
2023-09-14 09:07:28 时间





一、方法委托




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

在这里插入图片描述