知识库--The LifecycleSupport (45)

来源:互联网 发布:淘宝模特拍摄大牌风 编辑:程序博客网 时间:2024/05/19 17:47
     public final class LifecycleSupport{         public LifecycleSupport(Lifecycle){             super();             this.lifecycle = lifecycle;         }        private Lifecycle lifecycle = null;        private LifecycleListener listeners[] = new LifecycleListener[0];        public void addLifecycleListener(LifecycleListener listener){            synchronized(listeners){                LifecycleListener results[] = new LifecycleListener[listeners.length + 1];                for(int i = 0;i<listeners.length;i+){                    results[i] = listeners[i];                }                results[listeners.length] = listener;                listeners = results;            }        }        public LifecycleListener[] findLifecycleListeners(){            return listeners;        }        public fireLifecycleEvent(String type,Object data){            LifecycleEvent event = new LifecycleEvent(lifecycle,type,data);//使用lifecycle作为参数             LifecycleListener interested[] = null;            synchronized(listeners){                interested = (LifecycleListener)listeners.clone();            }            for(int i = 0;i < interested.length;i++){                interested[i].lifecycleEvent(event);            }        }        public void removeLifecycleListener(LifecycleListener listener){            synchronized(listeners){                int n = -1;                for(int i = 0;i<listeners.length;i++){                    if(listeners[i] == listener){                        n = i;                        break;                    }                }                if(n < 0){                    return;                }                LifecycleListener results[] = new LifecycleListener[listeners.length - 1];                int j = 0;                for(int i = 0; i < listeners.length;i++){                    if(i != n)                    results[j++]=listeners[i];                }                listeners = results;            }        }     }

When a listener is added in the addLifecycleListener method, a new array is created with the size of the number of elements in the old array plus one. Then, all elements from the old array are copied to the new array and the new listener is added. When a listener is removed in the removeLifecycleListener method, a new array is also created with the size of the number of elements in the old array minus one. Then, all elements except the one removed are copied to the new array.

The fireLifecycleEvent method fires a lifecyle event. First, it clones the listeners array. Then, it calls the lifecycleEvent method of each member, passing the triggered event.

A component implementing Lifecycle can use the LifecycleSupport class. For example, the SimpleContext class in the application accompanying this chapter declares the following variable:

    protected LifecycleSupport lifecycle = new LifecycleSupport(this);

To add a lifecycle listener, the SimpleContext calls the addLifecycleListener method of the LifecycleSupport class:

    public void addLifecycleListener(LifecycleListener listener){        lifecycle.addLifecycleListener(listener);    }

To remove a lifecycle listener, the SimpleContext class calls the removeLifecycleListener method of the LifecycleSupport class.
public void removeLifecycleListener(LifecycleListener listener){
lifecycle.removeLifecycleListener(listener);
}

To fire an event, the SimpleContext class calls the fireLifecycleEvent method of the LifecycleSupport class, such as in the following line of code:

    lifecycle.fireLifecycleEvent(START_EVENT,null);
0 0
原创粉丝点击