Andoid 总线设计 (三)
来源:互联网 发布:瓷砖排版软件下载 编辑:程序博客网 时间:2024/05/16 14:03
具体的代码设计:
标记注解 用于执行方法上面
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Subscriber {
SubscriberType type();
}
标记执行类型 同步或者异步
public enum SubscriberType {
ASYN, SYNC;
}
SubscribeMethod 保存目标函数的一个类:
里面有:
//执行对象
private Object object;
// 方法
private Method method;
// 参数 (只支持一个参数的方法)
private Class<> parameters;
//执行类型
private SubscriberType subscriberType;
省 get set
DistributeHandler 是一个分发接口
AsynDistributeHandler 异步分发实现。
SyncDistributeHandler 同步分发实现。
public interface DistributeHandler {
void handEvent(Object parameter);
}
public class AsynDistributeHandler implements DistributeHandler {
public AsynDistributeHandler(SubscribeMethod subscribeMethod) {
this.subscribeMethod = subscribeMethod;
}
private SubscribeMethod subscribeMethod = null;@Overridepublic void handEvent(final Object parameter) { new Thread(new Runnable() { @Override public void run() { ReflectionHelper.invokeSubscribeMethod(subscribeMethod, parameter); } }).start();}
}
public class SyncDistributeHandler implements DistributeHandler {
private SubscribeMethod subscribeMethod = null;public SyncDistributeHandler(SubscribeMethod subscribeMethod) { this.subscribeMethod = subscribeMethod;}@Overridepublic void handEvent(Object parameter) { ReflectionHelper.invokeSubscribeMethod(subscribeMethod, parameter);}
}
分发处理 管理类
DistributeManeger 管理事件的分发
public class DistributeManeger {
private DistributeHandler distributeHandler;
/** * 分发事件 * * @param parameter */public void post(Object parameter) { List<SubscribeMethod> subscribeMethods = ReflectionHelper.getTypeSubscribeMethods(parameter, EventBus.getInstance().getSubscribeMethodList()); for (SubscribeMethod subscribeMethod : subscribeMethods) { if (subscribeMethod.getSubscriberType().equals(SubscriberType.ASYN)) { distributeHandler = new AsynDistributeHandler(subscribeMethod); distributeHandler.handEvent(parameter); } else if (subscribeMethod.getSubscriberType().equals(SubscriberType.SYNC)) { distributeHandler = new SyncDistributeHandler(subscribeMethod); distributeHandler.handEvent(parameter); } }}/** * 注销 * * @param obj */public void unregister(Object obj) { List<SubscribeMethod> subscribeMethods = ReflectionHelper.getTypeObjSubscribeMethod(obj, EventBus.getInstance().getSubscribeMethodList()); for (SubscribeMethod subscribeMethod : subscribeMethods) { EventBus.getInstance().getSubscribeMethodList().remove(subscribeMethod); }}
}
反射处理类 ReflectionHelper :
public class ReflectionHelper {
private static final String TAG = ReflectionHelper.class.getSimpleName();
/** * 获取 含有 @Subscriber 的方法 * * @param obj * @param subscribeMethodList */public static void getSubscribeMethods(Object obj, List<SubscribeMethod> subscribeMethodList) { Class clazz = obj.getClass(); Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { if (method.isAnnotationPresent(Subscriber.class) && method.getParameterTypes().length == 1) { Subscriber subscriber = method.getAnnotation(Subscriber.class); Class parameters = method.getParameterTypes()[0]; SubscribeMethod subscribeMethod = new SubscribeMethod(); subscribeMethod.setObject(obj); subscribeMethod.setMethod(method); subscribeMethod.setParameters(parameters); subscribeMethod.setSubscriberType(subscriber.type()); subscribeMethodList.add(subscribeMethod); } }}/** * * 根据 Post 函数里面的参数 获取 有这些参数的方法 * @param parameter * @param subscribeMethodList * @return */public static List<SubscribeMethod> getTypeSubscribeMethods(Object parameter, List<SubscribeMethod> subscribeMethodList) { Class<?> clazz = parameter.getClass(); List<SubscribeMethod> typeSubscribeMethodList = new ArrayList<>(); for (SubscribeMethod subMethod : subscribeMethodList) { if (clazz.equals(subMethod.getParameters())) { typeSubscribeMethodList.add(subMethod); } } return typeSubscribeMethodList;}/** * 执行 * * @param subscribeMethod * @param parameter */public static void invokeSubscribeMethod(SubscribeMethod subscribeMethod, Object parameter) { Method method = subscribeMethod.getMethod(); Object object = subscribeMethod.getObject(); try { method.setAccessible(true); method.invoke(object, parameter); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); }}/** * 根据 obj 获取 列表 * * @param obj * @param subscribeMethodList * @return */public static List<SubscribeMethod> getTypeObjSubscribeMethod(Object obj, List<SubscribeMethod> subscribeMethodList) { List<SubscribeMethod> typeSubscribeMethodList = new ArrayList<>(); for (SubscribeMethod subMethod : subscribeMethodList) { if (obj.equals(subMethod.getObject())) { typeSubscribeMethodList.add(subMethod); } } return typeSubscribeMethodList;}
}
EventBus 事件处理类:
public class EventBus {
private static EventBus instance;
private List<SubscribeMethod> subscribeMethodList = new ArrayList<>();private DistributeManeger distributeManeger = new DistributeManeger();/** * @return */public static EventBus getInstance() { if (null == instance) newInstance(); return instance;}/** * 初始化实例 */private static synchronized void newInstance() { if (null == instance) { instance = new EventBus(); }}/** * 注册某对象 * 1 . 获取含有注解的方法 参数 * * @param obj */public void register(Object obj) { ReflectionHelper.getSubscribeMethods(obj, subscribeMethodList);}/** * 发送事件 * * @param obj */public void post(Object obj) { distributeManeger.post(obj);}/** * 反注册某对象 * * @param obj */public void unregister(Object obj) { distributeManeger.unregister(obj);}/** * * @return */public List<SubscribeMethod> getSubscribeMethodList() { return subscribeMethodList;}
}
好到此 代码写完了 接下来就是 测试及使用 最后总结一下。
- Andoid 总线设计 (三)
- Andoid 总线设计 (三)
- Andoid 总线设计 (一)
- Andoid 总线设计 (二)
- 基于MCP2515的Linux CAN总线驱动程序设计(三)
- 基于MCP2515的Linux CAN总线驱动程序设计(三)
- 基于MCP2515的Linux CAN总线驱动程序设计(三)
- 基于MCP2515的Linux CAN总线驱动程序设计(三)
- 基于MCP2515的Linux CAN总线驱动程序设计(三)
- 基于MCP2515的Linux CAN总线驱动程序设计(三)
- Andoid 采用ListView三种显示数据列表
- 三种总线
- 三条总线作用
- 三大总线
- 三条总线作用
- 平台总线(三)
- SOI总线(三)
- 消息总线授权设计
- 计算机系统进制备忘
- Binary Tree Longest Consecutive Sequence
- 初识Html问答
- Android 二维码扫描和生成简单实现
- 【操作系统】存储器管理01-思维导图
- Andoid 总线设计 (三)
- OkHttpUtils一个专注于让网络请求更简单的框架
- centos下redis安装
- Kafka初识
- 第六章 事件控制块
- QGIS二次开发包下载
- 第七天:整理S5PV210三种刷机方式 及自身问题总结
- jquery on方法
- HTML——转义字符