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

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.

主站蜘蛛池模板: 阳信县| 镇原县| 昌宁县| 福贡县| 屯门区| 皮山县| 开远市| 江都市| 静乐县| 城固县| 宜春市| 容城县| 陆丰市| 城口县| 广德县| 贺兰县| 漳州市| 平凉市| 黄浦区| 昭通市| 安图县| 连州市| 固始县| 山丹县| 甘谷县| 中卫市| 马鞍山市| 迁安市| 卢湾区| 富阳市| 厦门市| 高碑店市| 亳州市| 关岭| 民和| 闵行区| 龙胜| 西宁市| 金沙县| 安宁市| 渝中区|