Observer Pattern is defined by Gamma et al. as a "one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically".
They also say that the Observer pattern should be used in any of the following situations:
-
When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently.
-
When a change to one object requires changing others, and you don't know how many objects need to be changed.
-
When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don't want these objects tightly coupled.
On Adam Stroud Android Database Best Practices book it states that "the Cursor class provides the methods that expose the Observer pattern to a source of data. And, by using these methods, it is possible to respond to data changes using the Observer Pattern rather than polling the database for changes, which can be inefficient":
-
Cursor.registerContentObserver()
-
Cursor.unregisterContentObserver()
-
Cursor.setNotificationUri()
Similarly, by using a Content Provider, we can use a ContentResolver client object to access the data from the Content Provider and then register a ContentObserver to listen to the change of data behind the URI the observer was registered for.
So, as the Subject object in the Observer pattern, ContentResolver has the methods that, by my thinking, are almost the same:
-
registerContentObserver() of ContentResolver is Attach() from Subject
-
unregisterContentObserver() of ContentResolver is Dettach() from Subject
-
notifyChange() of ContentResolver is Notify() from Subject
So, my question is, if the three components of a ContentProvider (ContentProvider, ContentResolver, and ContentObserver) are by themselves an implementation of the Observer pattern?
Aucun commentaire:
Enregistrer un commentaire