Sunday, March 13, 2011

MVVM in Android

MVVM in Android
MVVM stands for Model-View-ViewModel, which is a pattern widely adopted in MS WPF and Silverlight. In this article, I am going to discuss the implementation of MVVM pattern in Android (Java) with the help of Android-Binding framework.

Source code for this article can be obtained here.

MVVM redefined for Android

Model: Model in Android can be data coming from within your application (incl. Shared Preferences), Database (in Cursor, or via other Data Access Object) or externally (via Cursor to other Data Contract)
View: all the elements displayed in GUI, that includes android.widget.* family, and your custom views
ViewModel: ViewModel exposes properties and commands for the View, it also serves in data binding between the View and the Model. In Android, most of this job is done in Activity
Implementation

We use a simple calculator as an example. As shown in following figure, the calculator consists of some buttons for input and operations, and a text box to display the result.

Read more: Codeproject