Skip to content

Instantly share code, notes, and snippets.

@hightemp
Last active May 7, 2017 17:09
Show Gist options
  • Save hightemp/a5b491ecaee309e04f94ae1036bd121d to your computer and use it in GitHub Desktop.
Save hightemp/a5b491ecaee309e04f94ae1036bd121d to your computer and use it in GitHub Desktop.
[JAVA] [Patterns] Simple observer pattern class
import java.lang.*;
import java.lang.reflect.*;
import java.util.*;
public static class SObserver {
protected static HashMap<String, Object> hmMethodsObjects = new HashMap<String, Object>();
protected static HashMap<String, Method> hmMethods = new HashMap<String, Method>();
protected static HashMap<String, String> hmListeners = new HashMap<String, String>();
// Method name, Listener group
private SObserver() {}
public static Boolean sfnIsMethod(String in_sMethodName) throws Exception {
return sfnGetMethodByString(in_sMethodName) != null;
}
public static Method sfnGetMethodByObject(Object in_objObject, String in_sMethodName) {
Method[] aMethods = in_objObject.getClass().getDeclaredMethods();
for (Method objMethod : aMethods) {
if (in_sMethodName.equals(objMethod.getName())) {
return objMethod;
}
}
return null;
}
public static Method sfnGetMethodByString(String in_sMethodName) throws Exception {
try {
String[] aMethodName = in_sMethodName.split("[.]");
String sMethodName = aMethodName[aMethodName.length-1];
String sClassName = "";
// TODO: Remove?
if (aMethodName.length > 2) {
aMethodName = Arrays.copyOfRange(aMethodName, 0, aMethodName.length-2);
sClassName = String.join(".", aMethodName);
} else if (aMethodName.length == 2)
sClassName = aMethodName[0];
// TODO: Remove?
Class objClass = Class.forName(sClassName);
Method[] aClassMethods = objClass.getDeclaredMethods();
for (int iIndex = 0; iIndex < aClassMethods.length; iIndex++) {
if (sMethodName.equals(aClassMethods[iIndex].getName())) {
Class[] aParameterTypes = aClassMethods[iIndex].getParameterTypes();
return objClass.getDeclaredMethod(sMethodName, aParameterTypes);
}
}
throw new RuntimeException("Method not found");
} catch (Throwable objException) {
throw new RuntimeException(objException.getMessage());
}
// return null;
}
public static void sfnAddStaticMethod(String in_sGroupName, String in_sMethodName) throws Exception {
if (sfnHasMethod(in_sMethodName))
return;
Method objMethod = sfnGetMethodByString(in_sMethodName);
if (objMethod != null) {
hmMethods.put(in_sMethodName, objMethod);
hmListeners.put(in_sMethodName, in_sGroupName);
}
}
public static void sfnAddMethod(Object in_objObject, String in_sGroupName, String in_sMethodName) throws Exception {
if (sfnHasMethod(in_sMethodName))
return;
Method objMethod = sfnGetMethodByObject(in_objObject, in_sMethodName);
if (objMethod != null) {
hmMethods.put(in_sMethodName, objMethod);
hmMethodsObjects.put(in_sMethodName, in_objObject);
hmListeners.put(in_sMethodName, in_sGroupName);
}
}
public static void sfnRemoveMethod(String in_sMethodName) throws Exception {
hmMethods.remove(in_sMethodName);
hmListeners.remove(in_sMethodName);
}
public static Boolean sfnHasMethod(String in_sMethodName) throws Exception {
return hmMethods.containsKey(in_sMethodName);
}
public static void sfnTriggerEvent(Event in_objEvent) throws Exception {
for (Map.Entry<String, String> objListenerEntry: hmListeners.entrySet()) {
String sListenerName = objListenerEntry.getValue();
String sListenerMethod = objListenerEntry.getKey();
Method objMethod = hmMethods.get(sListenerMethod);
Object objMethodObject = hmMethodsObjects.get(sListenerMethod);
if (objMethod != null) {
objMethod.invoke(objMethodObject, in_objEvent); //in_objEvent
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment