Abstraction with Java in Android

I studied some tutorials regarding the Java language. I was wondering if I should abbreviate every time I encode something, and on any type of standard and stack?

I saw that with every Spring Services, for example, we could even abstract controllers using interfaces with EJBs on the JavaEE stack, etc.

I was wondering what is the purpose? Should I do the same when developing with the Android SDK?

Should I abstract from each class that I code?

+7
java android abstraction refactoring modularity
source share
3 answers

It is always useful to create modular, reusable components. When an application is built from scratch with this in mind, it becomes more scalable, more self-expanding. The same components in the application are again used when adding new features, which saves time and effort. And it becomes easier to make changes later or to identify the sources of errors. Refactoring should never be later, but from the very beginning.

Having said that, it is not a good idea to have more and more abstractions in mobile applications just for the sake of “abstraction”. Of course, the reason is that smartphones are not as strong as servers or even desktop computers. There is a performance penalty associated with literally every class and virtual method in an Android app. There must be a greater balance between “abstraction” and efficiency, and performance trade-offs become more noticeable on mid-range and junior devices.

From official docs:

1. Be careful with code abstractions.

2. Avoid dependency injection infrastructures

3. Avoid creating unnecessary objects

4. Prefers static virtual

5. Avoid Internal Getters / Setters

EDIT:

After you recently tried out Dagger , I have to admit that point 2 may be a bit dated. What can I say ... I came to a party of daggers quite late.

+5
source share

You need an abstraction whenever you have a class that you do not want to implement with all its methods. Those classes that inherit it will be forced to implement all these methods, otherwise you would also need to declare subclasses as abstract.

In addition to this, you should know the interface, interface methods should not have a body, and it’s good that your class can implement as much as you need. Whereas you can only inherit one abstract class. Interfaces are like contracts. Whatever class executes them, they must provide a body for all their methods.

If you need an abstract or interface, or both really depend on your design and what you want to implement. Although it is good practice to force those classes that have common methods for implementing the same interface (if you know nothing about the body of each method) or abstract (if you know that the body is some kind, all or none of methods)

Another example might be when you have an abstraction or interface, if you add something to them, all subclasses or classes that implement them should follow these changes, which means it would be easier to get the changes.

See this , this, and this and the open / close principle .

+2
source share

This can be interpreted in different ways. From my point of view, abstraction is used in coding as a design principle, in cases where you need an extension or many types of implementations. For example, in Spring, a controller can be defined as an abstract class (A) and have several other controller types (B, C, D ..) that extend A. As a user of the Spring structure, if you do not satisfy the available controller implementations, however you can develop your own controller that extends A. Also, Spring developers can also easily expand / add new controllers in future versions.

+1
source share

All Articles