Reputation: 1361
In my app I have a single Activity architecture and for every Fragment I have defined a "View" class.
This "View" class contains the View state and the View actions, which refer to the view's data and actions.
For example, I have the "MainFragment.kt" and a relevant "MainView.kt" file that will be something like this:
object MainView {
data class MainState(...) {...}
sealed class Actions {
sealed class Navigate : Actions() {
object Back : Navigate()
data class NextScreen(...) : Navigate()
}
object DoSomething : Actions()
}
}
The "MainState" and the "Actions" are then imported and used in the "MainFragment.kt" and in its "ViewModel" class.
As you can see, "MainView" is defined as an object because I don't need to instantiate it. The way I use it I just use the data (state and actions) it contains. This is also a safer approach by design because I need to have only one instance of the "View" class existing at any time.
Possible Problem: However, since there are many Fragments and as a result many View classes that correspond to them, then obviously the app will have many objects. Of course these objects are statically allocated in memory and this may lead to increased memory use.
Question: Is there any better approach in how I can define my "View" classes? For example, if I define "MainView.kt" as a class (e.g. class MainView(){...}) instead of an object, will that be better in terms of memory allocation and performance?
Upvotes: 1
Views: 1876
Reputation: 93609
Don't worry about it. It's not like these object
class have properties holding megabytes worth of state. In your example above it has no state at all. Each of these object
s in your project is probably adding overhead of no more than 128 bytes (one word for the instance and one for the reference).
Suppose you had 100 of them in your project. You could reduce this overhead by changing your objects to classes (thereby making them advertise something you shouldn't do with them so your code is less clear). You would have saved less RAM than a line of text would use.
Upvotes: 3