java 在观察者模式中使用泛型T的实例

网友投稿 261 2023-06-09


java 在观察者模式中使用泛型T的实例

被观察者

public class Observable {

List observers = new ArrayList();

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;

}

/**

* ReturrnRXoLYshAns 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()}.

*

* 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 {

public void update(Observable observable, T data);

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:浅谈java 面对对象(抽象 继承 接口 多态)
下一篇:使用Spring的AbstractRoutingDataSource实现多数据源切换示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~