For my current project I have to create a sort of synchronization mechanism to upload some data to a web service (I’ve covered web services before on the blog). I’m going to complete that article by showing how to create a very simple synchronization manager that will let you communicate to your web service and the upper layers of your application using the Observer pattern.
It’s important to mention this is not a fully featured sync mechanisms because I will only show you how to do the upload process. Nevertheless with the bases presented on this article you will be able to easily implement these.
As I mention on the title, I’ll be using the Observer Pattern because of two reasons:
- I really don’t like the use of handlers in Java
- The Observer pattern is incredible easy to implement on the Android platform
Let’s say we have an application that lets you create notes. When you open the application you have a dashboard with different options like “Create new note”, “App settings”, “See my notes” and so. Once you are done creating a note, you go back to your dashboard where you can choose to see the notes you have created by going to the “See my notes” Activity. From there, there will be an icon that indicates if the note has been uploaded or not (because you may not have an internet connection all the time).
The problem we are trying to solve is not uploading the note: we are trying to do the process more elegant and to inform the activities in real time of the result of the syncing process. To do so, we will be using a few patterns: As the title says, we will use the Observer pattern which will allow us to forget about using Handlers to communicate between the manager and the activities. The other pattern we will be using is the Singleton pattern to avoid having multiple instances of the Manager.
If you are familiar with the Observer pattern, you know the Observable objects will send information to the observers. This information is usually in form of messages. We will create a Message class that will hold the information we believe is important.
Now lets create the SyncManager class
I’m not doing the upload methods because It exceeds the scope of the article. What’s important here is you to see how the manager tells the Observers what’s going on. You have to pay attention to the setChanged() and notifyObservers() methods.
There is a syncInProcess boolean that will prevent the cycle to enter again if we are already on it. Also I added a custom error.
Lets see how to use this on the Activities !
The code is really simple:
- onCreate we register this Activity as an observer
- onDestroy we do the inverse process to avoid having floating references all over the application
- the update() method is where the magic happens
When the Observables call the setChanged() and then notifyObservers(), the registered Observers will trigger the update() method. To make changes on the UI we have to use a Handler, bur in this case is contained within the Activity and not used as a parameter.
Wrapping it up
As you see, this is a very simple sync manager. There are several ways to improve it but I wanted to show the basics. I’d like to add this code can be easily migrated to other languages (like c# for example) because what’s important here is the concept.
Hope it helps !