Android Architectural patterns [closed]
Asked Answered
S

3

5

I have just started with the android development and I am trying to develop my first application, which I am actually going to publish. I have a programming background in Java and knowledge of some patterns however I have no idea which patterns I should stick with while developing android apps. Also where to put Threads. I am developing an app, which constantly loads data from a remote database through PHP scripts and displays them on UI. I divided an app to few layers - Presentation layer, Domain Layer/Service Layer and Data Source Layer. Between them I create facades to access the services of the layer bellow. I dont really know if I should stick with this structure or completely rebuild this app according to some other patters. Its better to find it out at the beginning of the development than to be forced to rebuild entire application later on. So if somebody could provide me with some links about architectural patterns which I can use or write something short about it here, I would really appreciate it!

Spiritualist answered 11/9, 2013 at 15:17 Comment(0)
E
0

You might want to put your network communication in a Service instead of AsyncTask or Thread. Your architecture sounds like some form of MVC which is good in my opinion.

I think the Activity is a good starting point for you. Learn it's lifecycle and how to present your data to the user. You can also read more about threads and connectivity to see for yourself how it's done in android.

Earn answered 2/10, 2013 at 11:11 Comment(2)
Activities violates MVC pattern, because its View and Controller is mixed together, But however the MVC is a good practice in AndroidRosannrosanna
@ABFORCE your answer is the best so far here, however I think this is best asked on the programmers SE as it suits that site better.Phenol
R
7

In my opinion the single responsibility principle and separating whole application into different layers (such as MVC pattern but Android is not fully compatible with formal MVC) is a good practice in Android development.Now I will talk about major layers in the following:

Representation layer :

For instance Android framework offers a very straightforward XML representation for Presentation layer,In regard to this XML representation, you should not create the user interface stuff in code. Instead, you must do it by XML.

Application Logic layer:

For application logic layer it is good to accomplish it in code, not anywhere else, For example there is a android:onclick="function_name" attribute in Android XML(for assigning an onClickListener to a View) But as MVC pattern the View/representation layer MUST be fully separated from Controller/logic layer.

Data source layer :

Finally you can have a data source layer which its responsibility is to providing data, persisting data, and all data related stuff. In Android you would put some things in this layer such as dealing with SQLite, ContentProviders, SharedPreferences etc

Result:

I think it's better to pick a main architecture pattern and design your application in high abstraction level according to your picked pattern and then implements its sub-layers. my favorite approach for architectural design and implementation is something sounds like top-down approach, in this strategy you would design your application in top to bottom manner / more abstract to less abstract / less detail to more detail

Rosannrosanna answered 8/2, 2014 at 12:6 Comment(0)
P
1

I divided an app to few layers - Presentation layer, Domain Layer/Service Layer and Data Source Layer.

Alternatively you could divide the app vertically by its features. So you get a package for each feature or activity, perhaps with subpackages. A good rule of thumb is: a package should not contain more logic, than you (or someone else) can easily understand. This technique has some advantages. First, your packages do not become bigger and bigger when you add more features to your app. Second, it becomes easier to maintain dependencies between different features. Perhaps your IDE can generate a dependency matrix of your packages.

Also where to put Threads. I am developing an app, which constantly loads data from a remote database through PHP scripts and displays them on UI.

Android has the concept of Loaders and AsyncTasks. They help you to seperate long running tasks from the UI. There is an example using the Loader-API on the Android developer website.

Python answered 11/9, 2013 at 15:53 Comment(1)
Async Tasks are aweful overall. They have a hard time with the Activity lifecycle. A service or intent service is a better choice as these exist outside of the activity lifecycle. You can use an Event Bus to create a message queue between the different threads.Phenol
E
0

You might want to put your network communication in a Service instead of AsyncTask or Thread. Your architecture sounds like some form of MVC which is good in my opinion.

I think the Activity is a good starting point for you. Learn it's lifecycle and how to present your data to the user. You can also read more about threads and connectivity to see for yourself how it's done in android.

Earn answered 2/10, 2013 at 11:11 Comment(2)
Activities violates MVC pattern, because its View and Controller is mixed together, But however the MVC is a good practice in AndroidRosannrosanna
@ABFORCE your answer is the best so far here, however I think this is best asked on the programmers SE as it suits that site better.Phenol

© 2022 - 2024 — McMap. All rights reserved.