Recently busy, long time no update blog, today will be a summary of recent research, recently in the maintenance of a company project, I responsible for the module is to download the magazine timely update the UI, update the latest download progress to the UI, a problem encountered in the middle: The download progress of the same object will have more than one item to listen to its status, that is, a progress to update to more than one item, the beginning of no clue, Baidu after discovering the observer pattern this thing, it is to meet this one-to-many problems and appear.
With the idea, start to understand how the observer pattern works, although it was known that the design pattern has this thing, but it is really useless, the following will explain how the observer pattern in the actual application of the project. Take a look at how the app works in your project:
: The same book in the bookstore page and the Bookshelf page download progress is exactly the same, we can use the Observer mode to achieve this effect.
1. What the Observer pattern contains: The Observer pattern mainly has the observer and the observer two kinds of objects, the observer mainly realizes the data or the state the update, has the update notifies all observers, but the observer is responsible for receives the notice, to update own concern the content.
2. How to use the Observer mode: 1) Create a Subject (topic interface), interface with all the functions that the observer wants to achieve; 2) create an observer interface, all of which implement this interface function, are called observers, 3) Register all observers that implement the observer interface with the Observer. 4 By iterating through all the observer objects, the observer invokes its implementation of the Observer interface method. (Is the callback procedure for an interface)
As long as you have achieved the above four steps to create a successful observer pattern, here is the framework structure of the observer pattern used in my project:
1. Create a subject theme interface that defines the features of the Observer.:
This defines the functions and additions of the observer, and removes the observer.
2. Create an Observer object to implement this topic interface functionality
Here, I omitted the other code in the project, leaving only a frame structure. So our observer class is created.
3. Create the Observer interface to define the functions that the viewer will implement.
4. Create our Observer class:
Realizing our well-defined observer interface becomes an observer.
At this point, the framework of the entire observer pattern is built, so long as the business logic we want is implemented in the corresponding class.
Summary: 1. The observer pattern is a very important design pattern, from the Android system design can be seen, the system design itself uses a lot of observer patterns, such as: view of the event monitoring mechanism, ContentProvider contentobserver , the baseadapter design, visible, the observer pattern is very important, is we must grasp. 2. The prerequisite for mastering the observer pattern is that the interface callback must be used skillfully, and the observer pattern is actually a complex interface callback.
Android design mode--viewer mode