【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 正常方法调用 | 方法委托实现 | 代码示例 )
2023-06-13 09:18:00 时间
文章目录
一、方法委托
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
相关文章
- JS对象的遍历方法
- Java的封装方法
- 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 实现 GroovyInterceptable 接口 | 重写 invokeMethod 方法 )
- 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 属性缺失 propertyMissing 函数回调 | 方法缺失 methodMissing 函数回调 )
- 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 @Category 注解进行方法注入 | 分类注入方法查找优先级 )
- 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 注入静态方法 )
- 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 分析使用 MetaClass 进行方法注入前后 mateClass 类型变化 )
- 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Mixin 混合进行方法注入 )
- Java Set.isEmpty()方法:判断Set集合对象是否为空
- JSP PageContext.getPage()方法:返回当前page对象
- JSP Request.getCookies()方法:获取cookie对象
- Hibernate delete方法:删除对象
- Spring get方法:获取指定ID编号的实体对象
- Linux 平台隐藏文件的简易方法(linux下隐藏文件)
- MySQL索引问题无法创建索引的解决方法(mysql不能创建索引)
- XMLDOM对象方法:对象属性
- js确定对象类型方法
- jquery获取对象的方法足以应付常见的各种类型的对象
- JavaScript判断变量是对象还是数组的方法