【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
相关文章
- 《Effective Java》第2章 对所有对象都通用的方法
- Struts2返回JSON对象的方法总结
- unity 改变鼠标样式的两种方法
- C# 直接调用非托管代码的方法
- IDEA 运行junit单元测试方法
- zone.js在bootstrap阶段对window对象里一些标准方法的注入逻辑
- 通过一个最简单的例子,理解Angular rxjs里的Observable对象的pipe方法
- js遍历对象数组map方法
- 【异常】记一次因注解@RestController没加(@RestController不会用),导致无法调用Controller层的方法
- 【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )
- 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入构造方法 )
- 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
- 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 属性缺失 propertyMissing 函数回调 | 方法缺失 methodMissing 函数回调 )
- 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法拦截 JDK 中已经定义的函数 )
- 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 使用 MetaClass 进行方法拦截 | 对象上拦截方法 | 类上拦截方法 )
- __getattribute__的妙用,实现对象实例的“继承“与方法重载
- Array对象的方法实现(4)----Array.prototype.findIndex和Array.prototype.forEach(实现常规参数的功能)
- Array对象的方法实现(5)----Array.prototype.includes(实现常规参数的功能)
- 【.Net实用方法总结】 整理并总结System.IO中MemoryStream类及其方法介绍