Burjua Burjua - 2 years ago 64
Android Question

Which design patterns are used on Android?

I'm doing a small research of mobile platforms and I would like to know which design patterns are used in Android?

e.g. in iOS Model-view-controller is very widely used together with delegation and other patterns.

What patterns and where in particular does Android use?


I'm not asking for design patterns used deep in kernel, dalvik and so on, but about patterns which an application developer will meet while developing an application.

Answer Source

I tried using both the Model-View-Control and Model-View-Presenter design patterns for doing android development. My findings are Model-View-Control works fine, but there is a couple of "issues". It all comes down to how you perceive android Activity class. Is it a controller, or is it a view?

The actual Activity class doesn't extend android's View class, but it does, however, handle displaying a window to the user and also handle the events of that window (onCreate, onPause etc).

This means, that when you are using a MVC pattern, your controller will actually be a pseudo View-Controller. Since it is handling displaying a window to the user, with the additional view components you have added to it with setContentView, and also handling events for atleast the various activity life cycle events.

In MVC, the controller is supposed to be the main entry point. Which is a bit debatable if this is the case when applying it to android development, since the activity is the natural entry point of most applications.

Because of this, i personall find that the Model-View-Presenter pattern is a perfect fit for android development. Since the Views role in this pattern are:

  • serving as a entry point
  • rendering components
  • routing user events to the presenter

This allows you to implement your model like so:

View - this contains your UI components, and handles events for them.

Presenter - this will handle communication between your model and your view, look at it as a gateway to your model. Meaning, if you have a complex domain model representing, God know what, and your view only needs a very small subset of this model, the presenters job is to query the model and then update the view. For example, if you have a model containing a paragraph of text, a headline and a word-count. But in a given view, you only need to display the headline in the view. Then the presenter will read the data needed from the model, and update the view accordingly.

Model - this should basically be your full domain model, hopefully it will help making your domain model more "tight" as well, since you wont need special methods to deal with cases as mentioned above.

By decoupling the model from the View all together (through use of the presenter), it also becomes much more intuitive to test your model. You can have unit test for your domain model, and unit test for your presenters.

Try it out, I personally find it a great fit for android development.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download