最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java 在观察者模式中使用泛型T的实例
时间:2022-06-29 01:39:59 编辑:袖梨 来源:一聚教程网
被观察者
| 代码如下 | 复制代码 | 
| publicclassObservable 
   List 
 booleanchanged =false; 
 
 /** * Adds the specified observer to the list of observers. If it is already * registered, it is not added a second time. * * @param observer * the Observer to add. */ publicvoidaddObserver(Observer observer) { if(observer ==null) { thrownewNullPointerException("observer == null"); } synchronized(this) { if(!observers.contains(observer)) observers.add(observer); } } 
 /** * Clears the changed flag for this {@code Observable}. After calling * {@code clearChanged()}, {@code hasChanged()} will return {@code false}. */ protectedvoidclearChanged() { changed =false; } 
 /** * Returns the number of observers registered to this {@code Observable}. * * @return the number of observers. */ publicintcountObservers() { returnobservers.size(); } 
 /** * Removes the specified observer from the list of observers. Passing null * won't do anything. * * @param observer * the observer to remove. */ publicsynchronizedvoiddeleteObserver(java.util.Observer observer) { observers.remove(observer); } 
 /** * Removes all observers from the list of observers. */ publicsynchronizedvoiddeleteObservers() { observers.clear(); } 
 /** * Returns the changed flag for this {@code Observable}. * * @return {@code true} when the changed flag for this {@code Observable} is * set, {@code false} otherwise. */ publicbooleanhasChanged() { returnchanged; } 
 /** * If {@code hasChanged()} returns {@code true}, calls the {@code update()} * method for every observer in the list of observers using null as the * argument. Afterwards, calls {@code clearChanged()}. * * Equivalent to calling {@code notifyObservers(null)}. */ publicvoidnotifyObservers() { notifyObservers(null); } 
 /** * If {@code hasChanged()} returns {@code true}, calls the {@code update()} * method for every Observer in the list of observers using the specified * argument. Afterwards calls {@code clearChanged()}. * * @param data * the argument passed to {@code update()}. */ publicvoidnotifyObservers(T data) { intsize =0; Observer[] arrays =null; synchronized(this) { if(hasChanged()) { clearChanged(); size = observers.size(); arrays =newObserver[size]; observers.toArray(arrays); } } if(arrays !=null) { for(Observer observer : arrays) { observer.update(this, data); } } } 
 /** * Sets the changed flag for this {@code Observable}. After calling * {@code setChanged()}, {@code hasChanged()} will return {@code true}. */ protectedvoidsetChanged() { changed =true; } } | |
观察者
| 代码如下 | 复制代码 | 
| publicinterfaceObserver   publicvoidupdate(Observable } | |
相关文章
- 暗喻幻想布丽吉塔设施完工时间说明 10-31
- 三国志8重制版居民情感作用介绍说明 10-31
- 三国志8重制版游戏灾害效果介绍说明 10-31
- 三国志8重制版武将不同状态区别说明 10-31
- 三国志8重制版武将阶级提升方法分享 10-31
- 三国志8重制版武将不同阶级作用说明 10-31
 
             
                                 
                                 
                                 
                                 
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                        