What is the observation of the pattern
When multiple observers want to observe the same subject object at the same time, the subject matter is the change of the state of our observer, and the way in which the observer can recall itself is old and updated.
Implementation steps: First: Define an observer interface The second observer implements the Observer interface
Third: If one is notified of the interface, there is Addobserver (Observer ob); Removeobver (Onbserver ob); Notifyobserver (Observer ob) method
Four: When the observed changes: Call Notifyobserver (Observer ob), iterate through the list, and callback the method inside the Observer.
# #我们常见的观察者模式的使用
The content provider in 1.Android takes the content Observer pattern:
The adapter of 2.RecyClerView and ListView is to take the observer mode;
3. When the two interfaces want to observe the download progress at the same time, you can use the Observer mode second to observe the advantages and disadvantages of the model
Advantages: A good decoupling of the notice and the observer, the observer does not need to understand how the temporal department is living to realize how to facilitate the future of the code changes, embodies the principle of dependence to turn.
Disadvantages:
1. Abstract notice or rely on the abstract observer, when there is no observer, there is no way with the new
2. All actions required of the observer must be the same, if not the same, cannot be achieved.
The
First is the summary of Rxjava: What is the first Rxjava, and what is the main thing for?
Second: Is the benefit of Rxjava and the rxabdroid of the next two
first: What is Rxjava:rxjava is: a library for composing ansynchronous and event-based p Rograms
Using observable sequences for Java VM (a library of asynchronous, event-based programs using an observable sequence on a Java VM); The
is mainly: Asynchronous understanding
In asynchronous we still use handle and Ansynctask are asynchronous,
His advantage is different from handle and Ansynctask said, the most concise is: simplicity as the program logic becomes more and more complex, He is still able to keep it simple.
//Detailed explanation of the Observer pattern: in observation: A policeman and a thief, the police (the observer) need to observe the action of the Thief (the observed),
react when the thief makes the theft, and the behavior is similar to that of the observer pattern, 10. A different place is to observe that there is no need to observe every moment;
Two are used to back out, when an action of the observed changes; Tell the observer by registering or subscribing; an action that is observed to change
make an example: Click event we set a click event for a button Button.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (view view) {
}
});
button in this is equivalent to a observed, and Onclicklistener is an observer, through the Setonclicklistener to establish a
relationship between the relations is actually subscribe to the relationship;
When the button is clicked, that is, the state changes, the framework will send the click event to the registered Onclicklistener;
The observable observer is the observed; Observer the callback method of the Observer Subscribe (SUBSCRIBE) Rxjava In addition to the
Ordinary event (OnNext () event) also defines two special events, respectively: The oncompleted () and onerror () events
represent errors in the process of event completion and event handling, and there is only one special event in a correctly running sequence of events, and the
last of the sequence of things.
Agent mode:
Follow up: The Aidl will involve: