【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )
2023-09-14 09:07:28 时间
一、批量方法委托
在上一篇博客 【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
相关文章
- 命令行中执行批量SQL的方法
- 修改资源管理器打开后的默认路径为计算机目录的方法
- IOS中获取各种文件的目录路径的方法
- 分享一个泛站站群域名百度站长平台批量认证域名方法
- java String/StringBuilder 方法
- 利用python批量修改word文件名的方法示例
- python使用datetime模块计算各种时间间隔的方法
- 【学习总结】SQL的学习-5-性能调优常用方法介绍与数据导入导出
- JS获取客户端IP地址、MAC和主机名七种方法
- 利用python批量修改word文件名的方法示例
- Effective Java -- 对于所有对象都通用的方法
- 数据库中批量插入数据的方法,记录一下。
- PHP执行zip与rar解压缩方法
- android 禁止第三方apk安装和卸载的方法详解
- Ipython:Ipython中Magic Functions魔法函数的简介、使用方法(单百分号%如%matplotlib inline、双百分号%%如%%!)之详细攻略
- Py之argparse:Python库之argparse(命令行解析)简介、安装、使用方法之详细攻略
- Dataset之LFW:LFW人脸数据库的简介、安装、使用方法之详细攻略
- WinForm-跨线程更新UI控件常用方法
- 批量标准化BN方法简介【避免了梯度消失和梯度爆炸、加速网络的收敛、优化网络结构】
- Laravel中resource方法
- python工具方法 18 labelme语义分割标注数据批量转换为png
- python工具方法 11 图像批量处理【灰度化、缩放、周边空白裁剪】