事件分发之扫描自定义注解实现观察者模式注册
2016-10-28 22:42
447 查看
一点机缘巧合,花了一个小时写了一版用反射方案去注册和释放的观察者设计模式,权当记忆一下吧
1、控制中心类
needscanner方法为扫描,notify为通知,remove为移除;
2、注解类
3、观察者类
4、main方法测试类
结果为:
后来发现有点想eventbus框架,不过大概思路差不多,就这样吧。。。
1、控制中心类
package org.iamwsbear.scannerobserver; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.*; /** * iamwsbear@gmail.com */ public class OberverControl { private static OberverControl oberverControl = new OberverControl(); public static OberverControl getInstance() { return oberverControl; } private Map<String, List<Object>> methodMap = new HashMap<String, List<Object>>(); public void needScanner(Object classa) { System.out.println("enter scanner method"); Annotation annotation = (classa).getClass().getAnnotation(NeedScanner.class); if (annotation != null) { System.out.println("annonation is equal"); Method[] methodList = classa.getClass().getMethods(); for (int j = 0; j < methodList.length; j++) { if (methodList[j].getName().startsWith("method")) { if (methodMap.get(methodList[j].getName()) != null) { methodMap.get(methodList[j].getName()).add(classa); } else { List<Object> list = new ArrayList<Object>(); list.add(classa); methodMap.put(methodList[j].getName(), list); } System.out.println("the class :" + classa.getClass().getSimpleName() + " method:" + methodList[j].getName() + "is register success"); } } } } public void notifyAllOberver(String method) { if (methodMap != null && methodMap.size() > 0) { Set<String> keySet = methodMap.keySet(); Iterator<String> iterator = keySet.iterator(); while (iterator.hasNext()) { String key = iterator.next(); if (method.equals(key)) { try { for (Object object : methodMap.get(key)) { Method shouldDoMethod = object.getClass().getMethod(method); shouldDoMethod.setAccessible(true); shouldDoMethod.invoke(object); } } catch (Exception e) { } } } } } public void removeObserver(Object classa) { if (methodMap != null && methodMap.size() > 0) { Map<String, Object> tempMap = new HashMap<String, Object>(); Set<String> keySet = methodMap.keySet(); Iterator<String> iterator = keySet.iterator(); while (iterator.hasNext()) { String key = iterator.next(); for (Object object : methodMap.get(key)) { if (object == classa) { tempMap.put(key, object); } } } if (tempMap.size() > 0) { Set<String> tempKeySet = tempMap.keySet(); Iterator<String> tempIterator = tempKeySet.iterator(); while (tempIterator.hasNext()) { String key = tempIterator.next(); methodMap.get(key).remove(tempMap.get(key)); System.out.println("the class :" + classa.getClass().getSimpleName() + " method:" + key + " is remove success"); } } } } }
needscanner方法为扫描,notify为通知,remove为移除;
2、注解类
package org.iamwsbear.scannerobserver; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * iamwsbear@gmail.com */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface NeedScanner { }
3、观察者类
package org.iamwsbear.scannerobserver; /** * iamwsbear@gmail.com */ @NeedScanner public class ObserverC { public ObserverC() { OberverControl.getInstance().needScanner(this); } public void methodA() { System.out.println("ObserverC methodA is doing"); } public void methodB() { System.out.println("ObserverC methodB is doing"); } public void detach() { OberverControl.getInstance().removeObserver(this); } }
4、main方法测试类
public class Main { public static void main(String[] args) { OberverControl.getInstance().notifyAllOberver("methodA"); OberverControl.getInstance().notifyAllOberver("methodB"); ObserverC observerC = new ObserverC(); ObserverD observerD = new ObserverD(); OberverControl.getInstance().notifyAllOberver("methodA"); OberverControl.getInstance().notifyAllOberver("methodB"); observerC.detach(); //observerD.detach(); OberverControl.getInstance().notifyAllOberver("methodA"); OberverControl.getInstance().notifyAllOberver("methodB"); } }
结果为:
后来发现有点想eventbus框架,不过大概思路差不多,就这样吧。。。
相关文章推荐
- js学习心得之js的自定义事件-基于观察者模式的实现
- ExtJS要利用观察者模式 去实现自定义的事件
- 利用接口实现自定义监听事件以及观察者模式
- 事件机制 -- 典型观察者模式 js实现
- Unity3d基于订阅者模式实现事件机制_解决装箱拆箱问题和注册的监听事件单一问题
- 传统javascript事件模型实现观察者模式
- 单例模式+观察者模式,60行代码简单实现事件总线。
- WCF中使用观察者模式实现事件通知
- spring 自定义注解的注册与扫描
- [Unity] 用观察者模式实现处理数据的分发
- C#中使用委托和事件实现观察者模式(observer pattern)
- 班主任来啦之观察者模式,事件委托等Java实现---如果你还不懂,看完此文,就一定会懂
- Spring自定义注解扫描的实现
- 用C#中的委托和事件简单实现观察者模式
- 使用委托和事件实现观察者模式(Observer Pattern)
- 委托 事件 实现观察者模式
- 使用委托和事件实现观察者模式(Observer Pattern)
- 使用UnrealScript实现基于观察者模式的事件系统
- java注解使用及使用注解实现观察者模式
- Android自定义View之用观察者模式写自定义监听事件以及常用竖直型字母索引栏的写法