What is the right way for Object Orient for an Android program?

Now I looked at a lot of tutorials on how to program for Android - I even started to create some programs myself. However, I noticed that my programs look like procedural ones, and Java - with object orientation. I tried to fix it, but I found a problem. The primary class of my program - the one that was launched at the beginning of the application (for example, in com.testprogram.www), is a mixture of screen and control layers at the same time.

In all the training programs that I found, I see that the visual object is being restored from the main.xml (for example, the button - this recovery tells me that this will be the โ€œcontrolโ€ layer for treatment) and right after that the object is registered for some or a listener (in this case, OnClickListener - this should be done on the screen, and not in the control, right?).

Does that mean that? This main class in the www package is what? "Screen" or "control"? Is this class the right place to do what I mentioned above? Is this done because the XML based interface cannot register Java listeners? Does anyone know a good place for me to ask for links on how to OO for Android?

+6
source share
4 answers

If you use standard layout templates like xml, strings in strings.xml, activity for handlers and place the main logic / algorithms / data warehouse in a separate class (model class), you will be good at writing the best code in your way.

To test your architecture,

1) Ask yourself if you can do a unit test in the main logic / algorithm / data store (model class) separately from the user interface. Can you reuse the model class in another project with a different interface without difficulty?

2) Then ask yourself if you can port the application to another language simply by providing an additional strings.xml file.

So, the presentation (View) is mainly presented in main.xml. Event code and processing code (Controller) mainly belongs to MyActivity.java Algorithm / data warehouse mainly refers to Model.java.

A really big separation between the main algorithm / data warehouse and the user interface. The underlying algorithm / data store should be unaware of the details of the user interface. On UNIX, this is the INTERFACE (VC) -ENGINE (M) pattern. Separating the view from the controller simply takes one step further.

Hope this helps, JAL

0
source

You can check two questions about two different architecture patterns.

+1
source

MVVM with this use of binding is very helpful in Android programming. This helps to reduce the overloaded activity structure that currently exists in the infrastructure. Below is a list of open source Binding lib that you can use.

http://code.google.com/p/android-binding/wiki/Motivation

0
source

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


All Articles