Observer Pattern is defined by the 'Gang of Four' Design Patterns book 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 ContentProvider, 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?
Even if I found some evidence that it could be an implementation of the Observer pattern, I would like a concrete answer explaining if it is really an implementation of the Observer pattern or not.
Yes, these components provide an implementation of the Observer Pattern.
The most important part of the design patterns described by the "Gang of Four" book are the concepts that they embody. Details such as the naming of the methods, classes, etc. often vary from implementation to implementation. Attach()/Detach()
, register()/unregister()
, watch()/unwatch()
, etc. Such choices ultimately matter relatively little. The key question is: does the implementation match the description that you quoted, i.e. is it a "one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically". I suggest that we clearly have that situation here. Additionally, having a class named ContentObserver
is also a good indication that we are indeed dealing with a version of the Observer Pattern.
Of course, there is an additional layer of complexity here in that the requirements of the Android API have complicated the structure of the Subject
by separating it into the ContentProvider
and ContentResolver
classes. In this case, the ContentProvider
is really the Subject
, but all interactions are required to be processed through ContentResolver
. Nevertheless, this complication doesn't change the fundamental nature of the behavior; it's still the Observer Pattern.