java 在觀察者模式中使用泛型T的實例
更新時間:2017年02月21日 11:05:05 投稿:jingxian
下面小編就為大家?guī)硪黄猨ava 在觀察者模式中使用泛型T的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
被觀察者
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的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java技巧分享之利用RxJava打造可觀測數(shù)據(jù)RxLiveData
這篇文章主要來和大家分享一個Java技巧,那就是利用RxJava打造可觀測數(shù)據(jù)RxLiveData,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-06-06
SpringBoot+MyBatis實現(xiàn)MD5加密數(shù)據(jù)庫用戶密碼的方法
MD5技術(shù)主要用于對用戶密碼加密,增加賬戶的安全性,他具有不可逆的特性,不會被輕易解密,這篇文章給大家介紹SpringBoot+MyBatis實現(xiàn)MD5加密數(shù)據(jù)庫用戶密碼的方法,感興趣的朋友跟隨小編一起看看吧2024-03-03
Struts2實現(xiàn)文件下載功能代碼分享(文件名中文轉(zhuǎn)碼)
這篇文章主要介紹了Struts2實現(xiàn)文件下載功能代碼分享(文件名中文轉(zhuǎn)碼)的相關(guān)資料,需要的朋友可以參考下2016-06-06
解決springboot 2.x集成log4j2調(diào)試日志無法關(guān)閉的問題
這篇文章主要介紹了解決springboot 2.x集成log4j2調(diào)試日志無法關(guān)閉的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

