官术网_书友最值得收藏!

The Observable interface

The java.util.Observable interface implements the Observer pattern, which can be co-related here. However, all similarities end here. If we look at the Observable interface, we have the following methods:

public class Observable {
void addObserver (Observer o);
void deleteObserver (Observer o);
void deleteObservers();
void notifyObservers();
void notifyObserver(int arg);
int countObservers();
boolean hasChanged();
}

Let's look at the Observer interface before we determine the differences:

public interface Observer{
void update(Observable o, Object arg)
}

If we look at the Observable and Observer interfaces, we can see that they are all about a single event and its state. The Observable API has the responsibility of determining a change and publishing it to all interested parties. On the other hand, the Observer only listens to the change. This is not what we are modeling with the Publisher and Subscriber interface. The Publisher interface is responsible for generating unbounded events, unlike the Observable, which is all about single entity state changes. The Subscriber, on the other hand, lists all kinds of events, such as data, error, and completion.

Furthermore, the Observable maintains an active list of observers. It has the responsibility of removing observers that are no longer interested in the event. This is not the same as the Publisher, which is only responsible for subscriptions. The Subscriber makes the decision to close the subscription, at its will.

主站蜘蛛池模板: 横山县| 比如县| 辽阳县| 紫阳县| 永泰县| 乐至县| 中超| 屏边| 兰坪| 聂拉木县| 霸州市| 彩票| 青铜峡市| 伊宁县| 马关县| 贵南县| 神农架林区| 神木县| 辽源市| 兴城市| 宿迁市| 翁牛特旗| 巴中市| 武安市| 鲁山县| 五原县| 永兴县| 靖西县| 乾安县| 裕民县| 永新县| 铁岭市| 柘荣县| 惠水县| 遂川县| 宾川县| 蒙城县| 武冈市| 阿巴嘎旗| 德江县| 阿克陶县|