java 在观察者模式中使用泛型T的实例
更新时间:2017年02月21日 11:05:05 投稿:jingxian
下面小编就为大家带来一篇java 在观察者模式中使用泛型T的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
被观察者
public class Observable<T> { List<Observer> observers = new ArrayList<Observer>(); boolean changed = 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. */ public void addObserver(Observer observer) { if (observer == null) { throw new NullPointerException("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}. */ protected void clearChanged() { changed = false; } /** * Returns the number of observers registered to this {@code Observable}. * * @return the number of observers. */ public int countObservers() { return observers.size(); } /** * Removes the specified observer from the list of observers. Passing null * won't do anything. * * @param observer * the observer to remove. */ public synchronized void deleteObserver(java.util.Observer observer) { observers.remove(observer); } /** * Removes all observers from the list of observers. */ public synchronized void deleteObservers() { 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. */ public boolean hasChanged() { return changed; } /** * 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()}. * <p> * Equivalent to calling {@code notifyObservers(null)}. */ public void notifyObservers() { 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()}. */ public void notifyObservers(T data) { int size = 0; Observer[] arrays = null; synchronized (this) { if (hasChanged()) { clearChanged(); size = observers.size(); arrays = new Observer[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}. */ protected void setChanged() { changed = true; } }
观察者
public interface Observer<T> { public void update(Observable<T> observable, T data); }
以上这篇java 在观察者模式中使用泛型T的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Java技巧分享之利用RxJava打造可观测数据RxLiveData
这篇文章主要来和大家分享一个Java技巧,那就是利用RxJava打造可观测数据RxLiveData,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下2023-06-06SpringBoot+MyBatis实现MD5加密数据库用户密码的方法
MD5技术主要用于对用户密码加密,增加账户的安全性,他具有不可逆的特性,不会被轻易解密,这篇文章给大家介绍SpringBoot+MyBatis实现MD5加密数据库用户密码的方法,感兴趣的朋友跟随小编一起看看吧2024-03-03解决springboot 2.x集成log4j2调试日志无法关闭的问题
这篇文章主要介绍了解决springboot 2.x集成log4j2调试日志无法关闭的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-07-07
最新评论