Model/View/ViewModel is a variation of Model/View/Controller (MVC) that is tailored for modern UI development platforms where the View is the responsibility of a designer rather than a classic developer. The designer is generally a more graphical, artistic focused person, and does less classic coding than a traditional developer.
MVVM is a variation of Martin Fowler's Presentation Model design pattern. MVVM abstracts a view's state and behavior in the same way, but a Presentation Model abstracts a view (creates a view model) in a manner not dependent on a specific user-interface platform.
- kotlin
- coroutine
- single activity
- architecture component
- navigation component + fragment
- presentetion layer(per page) = fragment + view model
- reactive ui = live data + data binding
- data layer = repositpory + local(room) + remote
- datalayer one shot operations(no listener or data streams)
suspend fun showSomeData() = coroutineScope {
val data = async(Dispatchers.IO) { // <- extension on current scope
// ... load some UI data for the Main thread ...
}
withContext(Dispatchers.Main) {
doSomeWork()
val result = data.await()
display(result)
}
}
We clarified comparing basic Kotlin classes with Java classes about properties and fields.
Property
// Kotlin
class Human {
val age = 20
}
// Decompiled to Java
public final class Human {
private final int age = 20;
public final int getAge() {
return this.age;
}
}
// Kotlin
class Human {
var age = 20
}
// Decompiled to Java
public final class Human {
private int age = 20;
public final int getAge() {
return this.age;
}
public final void setAge(int var1) {
this.age = var1;
}
}
Backing field
// Kotlin
class Human {
var age = 20
set(value) {
field = value
}
}
// Decompiled to Java
public final class Human {
private int age = 20;
public final int getAge() {
return this.age;
}
public final void setAge(int value) {
this.age = value;
}
}
Backing property
// Kotlin
class Human {
private var _age: Int = 20
val age: Int
get() {
return _age
}
fun setAge(value: Int) {
_age = value
}
}
// Decompiled to Java
public final class Human {
private int _age = 20;
public final int getAge() {
return this._age;
}
public final void setAge(int value) {
this._age = value;
}
}
Data class
// Kotlin
data class Human(val age: Int = 20)
// Decompiled to Java
public final class Human {
private final int age;
public final int getAge() {
return this.age;
}
public Human(int age) {
this.age = age;
}
// $FF: synthetic method
public Human(int var1, int var2, DefaultConstructorMarker var3) {
if ((var2 & 1) != 0) {
var1 = 20;
}
this(var1);
}
public Human() {
this(0, 1, (DefaultConstructorMarker)null);
}
public final int component1() {
return this.age;
}
@NotNull
public final Human copy(int age) {
return new Human(age);
}
// $FF: synthetic method
@NotNull
public static Human copy$default(Human var0, int var1, int var2, Object var3) {
if ((var2 & 1) != 0) {
var1 = var0.age;
}
return var0.copy(var1);
}
@NotNull
public String toString() {
return "Human(age=" + this.age + ")";
}
public int hashCode() {
return this.age;
}
public boolean equals(@Nullable Object var1) {
if (this != var1) {
if (var1 instanceof Human) {
Human var2 = (Human)var1;
if (this.age == var2.age) {
return true;
}
}
return false;
} else {
return true;
}
}
}
Below you can see how to decompile your class on Android Studio!!