My architecture of restful Android project

Overview

Currently, Android phone is very common device and it becomes a trend of future. And restful api and Android phone is a good choice for many projects. When I worked on many projects, I got a lot of issues because I put wrong thing to wrong position on code base. After all, I create an architecture for restful Android application.

Architecture

569843f4-a7c3-11e4-93bd-4b4ce75daa1e

Controller:

  • Activities
  • Fragments
  • Holders

Business: Business part has two main goals:

  • Managing network
  • Managing datasource

Network:

  • Calling api
  • Uploading image

Datasource:

  • Using ORM library

Note

  • Controller pass callback to Business.
  • Business pass callback to Network.

Example Client creates a new book. This action will be followed steps:

  • Controller calls BookBusiness.createBook() method.
  • BookBusiness.createBook() method will do below steps:
    • BookNetwork.creteBook()
    • If server creates book fail, BookNetwork will call onNetworkFailure() andBookBusiness will call onCreateBookFailure() to showing error on activity.
    • If server creates book successfully, BookNetwork will call onNetworkSuccess() andBookBusiness call BookDatasource.createBook(). AfterBookDatasource.createBook() creates book on database, returning book (new book variable) to controller.

Advantages:

  • Move Network logic outside of Controller's code
  • Network logic will be isolated.
  • All logic will be belonged to Business layer
  • Controller will be thin. Controller has only goals:
    • Binding component views (2-way binding)
    • Dispatching UI actions
  • Controller can be later changed to MVVM easier.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s