42

I'm working on an Android project and I would like to know any recommendations about what's a good architecture to build an android application.

I want to use dependency injection using Roboguice and I've been reading about MVVM pattern or MVC pattern (Android MVVM Design Pattern Examples).

Also I know that roboguice have a pretty cool Context-Based Event's raising and handling feature that could be very testable as the code is decoupled.

Any recommendations on a working design pattern? a testable and scalable architecture you have worked with or developed?

Community
  • 1
  • 1
Jose R. Cruz
  • 909
  • 2
  • 9
  • 15
  • 2
    Those are both good ones for any UI application, and DI is particularly useful for testability. But most design patterns are situational, so you're going to have to write some code and figure out some lower-level problems you want to solve before you're going to find many patterns to be useful. – Merlyn Morgan-Graham Jun 05 '11 at 20:43
  • If you search here for ["android design-patterns architecture"](http://stackoverflow.com/search?q=android+design-patterns+architecture) you may find some guidance. – DOK Jun 05 '11 at 20:50
  • If you look at the GoF book you will see that patterns will be used in various places. Your Activities may be a controller pattern (should), and the content provider will be a data access pattern. You will find other places where patterns can be useful, but, as mentioned, it depends on the situation as to the patterns to use. – James Black Jun 05 '11 at 21:06
  • 4
    Application should be a singleton. – Snicolas Jun 06 '11 at 22:37
  • Thanks for all your comments, I'll try to dig into the Activities as Controllers so code is cleaner, my past experience was with a bunch of code on the activity and that was kind of hard to test – Jose R. Cruz Jun 08 '11 at 16:27

1 Answers1

18

The Android platform provides a common set of design patterns, and with the limited hardware resources you get compared to Web-apps it is still often best to stick with using these directly in production code. There are other frameworks that sort of "wrap" the base platform; these are worth looking into if you have a specific purpose (or perhaps for prototyping/experimenting), but for the best level of support you are generally best sticking with the standard components.

This is a great resource when working on UI solutions: http://www.androidpatterns.com/

Specifically for DI: There is a Spring framework for Android, I've had a play with it and it looks quite promising. You've already mentioned Roboguice as another alternative to this. However, to avoid performance and library overhead, I still find the easiest approach is to write a simple reflection-based class that registers and injects dependencies within my own code. Similar to this approach, except I usually move the injection code into a separate singleton and reference it from there.

In my experience most of the third-party offerings are not yet mature enough to rely on right now, and don't really give you much on top of what the base platform provides. They are constantly progressing, however, so be sure to experiment with the big names from time-to-time.

seanhodges
  • 17,426
  • 15
  • 71
  • 93
  • 5
    Great answer! Spring Android is great, but it currently only provide integration libs, there's no DI in there AFAIK. RoboGuice is good too, but the Reflection API comes with a performance cost on some devices. [AndroidAnnotations](http://github.com/excilys/androidannotations) does the same kind of stuff, at compile time. Would love to hear your feedback on that :). – Pierre-Yves Ricau Jan 14 '12 at 16:03
  • 1
    AndroidAnnotations does look very interesting. I've bookmarked it for next time I'm working on an Android app, will drop some feedback once I get chance to try it. – seanhodges Jan 19 '12 at 11:55