您的位置:首页 > 其它

事件分发之扫描自定义注解实现观察者模式注册

2016-10-28 22:42 447 查看
一点机缘巧合,花了一个小时写了一版用反射方案去注册和释放的观察者设计模式,权当记忆一下吧

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框架,不过大概思路差不多,就这样吧。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息