How to properly structure your Android application, classes, views, databases, simple?

I have damn time grabbing the best way to โ€œpackโ€ my Android application into logical components, my goal is to make an application that is easy to update and add new features.

I believe that the โ€œmodularโ€ approach will work best: one, where my data is presented as data classes, my database has its own DB Helper class, which processes all interactions with the database and, finally, considers the โ€œControllerโ€ activity classes, which interact with DB Helper, data classes and views together.

At the same time, I need some high-level rules to structure my programming, I believe that if I do not have rules, I get sloppy code and massive rewrites as the code becomes too complicated.

All things discussed, are these rules a good foundation for an SQL-enabled application?

My theoretical Android rules:

  • The DB Helper class must contain all the database logic. This means that DB Helper will contain code for opening and closing the database, as well as code for inserting, updating, and deleting all records. DB Helper will return the data not as cursors, but as objects of the data class that I create, each data class will have a constructor that takes a cursor and uses it to populate the class values.

  • Data classes allow you to represent DB records as objects, not just cursors. Since DB Helper will only return objects, I will have to create adapters for elements such as ListView to display my object data as opposed to the cursor (I'm a little suspicious in this matter, not sure if this is a good idea or not). All business logic, therefore, all calculations that must be performed on the data contained in the data class will be performed in the data class itself. I assume that my data classes have typical getters and setters, as well as "calculation" methods, these calculation methods will take vars from the data class and do some meaningful business logic on them, returning the result (is this a good idea?).

  • The activity classes of management classes will literally associate DB Helper methods with data classes and update views using the information received. This activity class will be quite normal, it will initialize the layout and widgets, it will use lifecycle methods to execute various queries in the database through the database assistant at the appropriate time points, and it will have simple update methods that update widgets using the data class object, which they receive.

I find that I have no problems with Android, except for problems with such design problems. I'm tired of the fact that my applications are becoming too complex, and I need a simple system so that everything remains manageable and extensible.

If you fought like me, please, please, please push me in the right direction to structure the application. That's all that keeps me from creating what I hope is awesome apps for every pleasure.

+4
source share
1 answer

The most important thing you need to consider when developing an application: who will support it? If you are going to be the only one who works with your application, then do everything that is best for you. However, if others will support this application, then yes, you will want to keep similar things together. You seem to have a good plan. Make sure you use comments, and if you want, you can include "readme.txt", which will allow others to see what is happening and explain your design logic. To keep things from getting too complicated, you can use packages to store such classes.

Ultimately, there is no single correct answer to your question.

+1
source

Source: https://habr.com/ru/post/1433019/


All Articles