java 通用事件回调类(观察者队列)
来源:互联网 发布:演讲学而知不足 编辑:程序博客网 时间:2024/06/07 07:13
//文件名 CCommonDelegate.java
//作者 龙远智 @imo云办公室
//邮件 lyzlyz_2004@126.com
//时间 2014.Aug.5
//功能 委托库 CCommonDelegate
//声明 您可以自由使用和修改本函数库,但请保留本文件头
package com.imo.base;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.Iterator;
public class CCommonDelegate {
private Class<?>[] paramsType=null;
private class InnStruct<T>
{
public Method m_mthod = null;
public T m_objRef = null;
private Class<?> classzz=null;
InnStruct(T o, Method m)
{
classzz = o.getClass();
m_mthod = m;
m_objRef = o;
}
}
LinkedList<InnStruct<?>> m_lsObservers = new LinkedList<InnStruct<?>>();
public CCommonDelegate(Class<?>[] paramsType)
{
this.paramsType=paramsType;
}
public <T> boolean Bind(T o, String sMethodName)
{
assert(paramsType != null);
if(hasBind(o))
return true;
Method m = null;
try
{
m = o.getClass().getMethod(sMethodName, paramsType);
}
catch(NoSuchMethodException e)
{
e.printStackTrace();
return false;
}
catch(SecurityException e)
{
e.printStackTrace();
return false;
}
InnStruct<T> obj = new InnStruct<T>(o, m);
synchronized(this)
{
m_lsObservers.add(obj);
}
return true;
}
public boolean hasBind(Object o)
{
synchronized(this)
{
Iterator<InnStruct<?>> it = m_lsObservers.iterator();
for(; it.hasNext();)
{
if(it.next().m_objRef == o)
{
return true;
}
}
return false;
}
}
public void UnBind(Object o)
{
synchronized(this)
{
Iterator<InnStruct<?>> it = m_lsObservers.iterator();
for(; it.hasNext();)
{
if(it.next().m_objRef == o)
{
it.remove();
return;
}
}
}
}
public void clear()
{
synchronized(this)
{
m_lsObservers.clear();
}
}
public void invoke(Object... params) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
boolean bEqual = arrayContentsEq(paramsType, params, false);
if(!bEqual)
{
CheckParamTypesError(params);
}
CallObservers(params);
}
public void invokeWithSubClassTolerant(Object... params) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
boolean bEqual = arrayContentsEq(paramsType, params, true);
if(!bEqual)
{
CheckParamTypesError(params);
}
CallObservers(params);
}
private void CheckParamTypesError(Object... params) throws IllegalArgumentException
{
StringBuffer sDesc = new StringBuffer("CCommonDelegate invoke paramters type mismatch, expect:(");
int i=0;
for(i=0;i<paramsType.length;++i)
{
sDesc.append(paramsType[i].getName()).append(",");
}
sDesc.append("), but get:(");
for(i=0;i<params.length;++i)
{
sDesc.append(params[i].getClass().getName()).append(",");
}
sDesc.append(")");
throw new IllegalArgumentException(sDesc.toString());
}
private void CallObservers(Object... params) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
LinkedList<InnStruct<?>> oListTmp = null;
synchronized(this)
{
oListTmp = new LinkedList<InnStruct<?>>(m_lsObservers);
}
for(InnStruct<?> oRef:oListTmp)
{
oRef.m_mthod.invoke(oRef.m_objRef, params);
}
oListTmp.clear();
oListTmp = null;
}
private static boolean arrayContentsEq(Class<?>[] a1, Object[] a2, boolean bSubClassTolerant)
{
if (a1 == null)
{
return a2 == null || a2.length == 0;
}
if (a2 == null)
{
return a1.length == 0;
}
if (a1.length != a2.length)
{
return false;
}
for (int i = 0; i < a1.length; i++)
{
if(bSubClassTolerant)
{
if (a2[i] != null && !a1[i].isInstance(a2[i]))
{
return false;
}
}
else
{
if (a2[i] != null && a1[i] != a2[i].getClass())
{
return false;
}
}
}
return true;
}
}
//作者 龙远智 @imo云办公室
//邮件 lyzlyz_2004@126.com
//时间 2014.Aug.5
//功能 委托库 CCommonDelegate
//声明 您可以自由使用和修改本函数库,但请保留本文件头
package com.imo.base;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.Iterator;
public class CCommonDelegate {
private Class<?>[] paramsType=null;
private class InnStruct<T>
{
public Method m_mthod = null;
public T m_objRef = null;
private Class<?> classzz=null;
InnStruct(T o, Method m)
{
classzz = o.getClass();
m_mthod = m;
m_objRef = o;
}
}
LinkedList<InnStruct<?>> m_lsObservers = new LinkedList<InnStruct<?>>();
public CCommonDelegate(Class<?>[] paramsType)
{
this.paramsType=paramsType;
}
public <T> boolean Bind(T o, String sMethodName)
{
assert(paramsType != null);
if(hasBind(o))
return true;
Method m = null;
try
{
m = o.getClass().getMethod(sMethodName, paramsType);
}
catch(NoSuchMethodException e)
{
e.printStackTrace();
return false;
}
catch(SecurityException e)
{
e.printStackTrace();
return false;
}
InnStruct<T> obj = new InnStruct<T>(o, m);
synchronized(this)
{
m_lsObservers.add(obj);
}
return true;
}
public boolean hasBind(Object o)
{
synchronized(this)
{
Iterator<InnStruct<?>> it = m_lsObservers.iterator();
for(; it.hasNext();)
{
if(it.next().m_objRef == o)
{
return true;
}
}
return false;
}
}
public void UnBind(Object o)
{
synchronized(this)
{
Iterator<InnStruct<?>> it = m_lsObservers.iterator();
for(; it.hasNext();)
{
if(it.next().m_objRef == o)
{
it.remove();
return;
}
}
}
}
public void clear()
{
synchronized(this)
{
m_lsObservers.clear();
}
}
public void invoke(Object... params) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
boolean bEqual = arrayContentsEq(paramsType, params, false);
if(!bEqual)
{
CheckParamTypesError(params);
}
CallObservers(params);
}
public void invokeWithSubClassTolerant(Object... params) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
boolean bEqual = arrayContentsEq(paramsType, params, true);
if(!bEqual)
{
CheckParamTypesError(params);
}
CallObservers(params);
}
private void CheckParamTypesError(Object... params) throws IllegalArgumentException
{
StringBuffer sDesc = new StringBuffer("CCommonDelegate invoke paramters type mismatch, expect:(");
int i=0;
for(i=0;i<paramsType.length;++i)
{
sDesc.append(paramsType[i].getName()).append(",");
}
sDesc.append("), but get:(");
for(i=0;i<params.length;++i)
{
sDesc.append(params[i].getClass().getName()).append(",");
}
sDesc.append(")");
throw new IllegalArgumentException(sDesc.toString());
}
private void CallObservers(Object... params) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
LinkedList<InnStruct<?>> oListTmp = null;
synchronized(this)
{
oListTmp = new LinkedList<InnStruct<?>>(m_lsObservers);
}
for(InnStruct<?> oRef:oListTmp)
{
oRef.m_mthod.invoke(oRef.m_objRef, params);
}
oListTmp.clear();
oListTmp = null;
}
private static boolean arrayContentsEq(Class<?>[] a1, Object[] a2, boolean bSubClassTolerant)
{
if (a1 == null)
{
return a2 == null || a2.length == 0;
}
if (a2 == null)
{
return a1.length == 0;
}
if (a1.length != a2.length)
{
return false;
}
for (int i = 0; i < a1.length; i++)
{
if(bSubClassTolerant)
{
if (a2[i] != null && !a1[i].isInstance(a2[i]))
{
return false;
}
}
else
{
if (a2[i] != null && a1[i] != a2[i].getClass())
{
return false;
}
}
}
return true;
}
}
使用:
class A
{
public CCommonDelegate evt_OnNetWorkException = new CCommonDelegate(new Class<?>[]{Integer.class});
private void func
{
evt_OnNetWorkException.invoke(1);
}
}
class B
{
public void onNetworkException(Integer nErrcode)
{
......
}
public B(A objA)
{
objA.evt_OnNetWorkException.Bind(this, "onNetworkException");
}
}
0 0
- java 通用事件回调类(观察者队列)
- java事件处理机制(自定义事件)/观察者模式
- 观察者 (事件委托)
- 【java】事件与观察者模式
- 观察者设计模式 Vs 事件委托(java)
- 观察者设计模式 Vs 事件委托(java)
- Java设计模式——观察者模式(事件监听)
- 观察者设计模式 Vs 事件委托(java)
- c#消息队列,事件处理(java)
- 自定义事件(观察者模式)
- java中的事件监听器和观察者模式
- 观察者模式(Java)
- Java(观察者模式)
- 对象间的联动——观察者模式与Java事件处理(五)
- java事件监听机制(观察者设计模式的实际运用)
- c语言实现通用数据结构(二):通用队列
- 观察者模式(事件监听实现)
- 事件 委拖 及应用(观察者模式)
- android:onClick="XXX"的使用
- python如何处理异常
- 一个有效背英语单词的方法
- git 在linux中的使用
- JSP中对date对象的操作,如获取时分秒
- java 通用事件回调类(观察者队列)
- cas sso 单点登录
- 几种TCP连接中出现RST的情况
- 如何在QML中设计一个expandable ListView
- Mantle 初步使用
- windows7家庭普通版创建administrator用户
- awk学习笔记
- linux内存操作-----ioremap
- Myeclipse j2ee版本切换