当前位置 : 主页 > 编程语言 > java >

一个java 通知

来源:互联网 收集:自由互联 发布时间:2021-06-28
NSNotificationCenter.java package com.NSNotification;import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;public class NSNotificationCen
NSNotificationCenter.java
package com.NSNotification;
import java.lang.reflect.Method; 
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class NSNotificationCenter {

	
	private static NSNotificationCenter signle = null;
	
	private Map
 
  > nameMap;			//名字数组

	private HashMap
  
   > objectMap; //对像数据 private NSNotificationCenter() { this.nameMap = new HashMap
   
    >(); this.objectMap = new HashMap
    
     >(); } public static NSNotificationCenter defaultCenter() { if (signle == null) { signle = new NSNotificationCenter(); } return signle; } public void addObserver(Object object,Method method,String name) { if(object == null || method == null || name == null) { System.out.print("传入的值不能为空"); return; } ArrayList
     
       nameValue = nameMap.get(name); if(nameValue == null) { nameValue = new ArrayList
      
       (); } //存储数组到字典中 NSNotificationObject notificationObject = new NSNotificationObject(object, method, name); nameValue.add(notificationObject); nameMap.put(name, nameValue); //存储object 数组 ArrayList
       
         objectList = objectMap.get(object); if (objectList == null) { objectList = new ArrayList
        
         (); } objectList.add(notificationObject); objectMap.put(object, objectList); } // public void postNotificationName(String name) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { ArrayList
         
           nameValue = this.nameMap.get(name); if (nameValue != null) { for (int i = 0; i < nameValue.size(); i++) { NSNotificationObject object = nameValue.get(i); object.method.invoke(object.object); } } } public void postNotificationName(String name,Map
          
           map) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { ArrayList
           
             nameValue = this.nameMap.get(name); if (nameValue != null) { for (int i = 0; i < nameValue.size(); i++) { NSNotificationObject object = nameValue.get(i); NSNotification notification = new NSNotification(name, map); System.out.print(object.method); object.method.invoke(object.object,(Object)notification); } } } //移除对像所有监听 public void removeObserver(Object observer) { ArrayList
            
              objectList = objectMap.get(observer); if (objectList != null) { for (int i = 0; i < objectList.size(); i++) { NSNotificationObject notificationObject = objectList.get(i); ArrayList
             
               nameValue = nameMap.get(notificationObject.nameString); if (nameValue != null) { if(nameValue.contains(notificationObject)) { nameValue.remove(notificationObject); } } } objectMap.remove(observer); } } //移除对像某一个监听 public void removeObserver(Object observer,String name) { ArrayList
              
                objectList = objectMap.get(observer); if (objectList != null) { for (int i = 0; i < objectList.size(); i++) { NSNotificationObject notificationObject = objectList.get(i); ArrayList
               
                 nameValue = nameMap.get(name); if (nameValue != null && notificationObject.nameString.equals(name)) { if(nameValue.contains(notificationObject)) { nameValue.remove(notificationObject); objectList.remove(notificationObject); } } } } } private class NSNotificationObject { public Object object; public String nameString; public Method method; public NSNotificationObject(Object object , Method method, String name) { this.object = object; this.method = method; this.nameString = name; } } }
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 
Test.java
package com.NSNotification;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


public class Test {
//	 @SuppressWarnings("rawtypes")
	    public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {

		 Test textTest = new Test();
		 Class
 
   clazz = (Class
  
   ) textTest.getClass(); Method method = clazz.getMethod("objectText1"); NSNotificationCenter.defaultCenter().addObserver(textTest, method, "huang"); NSNotificationCenter.defaultCenter().postNotificationName("huang"); try { // parameter type is null Method method2 = clazz.getDeclaredMethod("objectText2",NSNotification.class); NSNotificationCenter.defaultCenter().addObserver(textTest, method2, "huang2"); Map
   
     map = new HashMap
    
     (); map.put("1111", "2222"); NSNotificationCenter.defaultCenter().postNotificationName("huang2",map); } catch(NoSuchMethodException e) { System.out.println(e.toString()); } } public void objectText2(NSNotification notification) { System.out.print("测试成功2"); System.err.print(notification.userInfo()); System.out.print("测试成功3"); } public void objectText1() { System.out.print("测试成功1"); } protected void finalize() { System.out.println("~A()"); } }
    
   
  
 
网友评论