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)
}
}
// Kotlin
var counter = 0
set(value) {
if (value >= 0) field = value
}
// Kotlin
class Human {
val age = 20
get() {
println("Age is: $field")
return field
}
}
// Java
public final class Human {
private final int age = 20;
public final int getAge() {
String var1 = "Age is: " + this.age;
System.out.println(var1);
return this.age;
}
}
バッキング・プロパティ
ゲッターやセッターの外でフィールドに直接アクセスしたいときなどに使います。
private なフィールドに_を付けて明示します。
// Kotlin
private var _table: Map<String, Int>? = null
public val table: Map<String, Int>
get() {
if (_table == null) {
_table = HashMap() // Type parameters are inferred
}
return _table ?: throw AssertionError("Set to null by another thread")
}
// Kotlin
class Human {
private val _age: Int = 20
val age: Int
get() {
return _age
}
val printAge = {
println("Age is: $_age")
}
}
// Java
public final class Human {
private final int _age = 20;
@NotNull
private final Function0 printAge = (Function0)(new Function0() {
public Object invoke() {
this.invoke();
return Unit.INSTANCE;
}
public final void invoke() {
String var1 = "Age is: " + Human.this._age;
System.out.println(var1);
}
});
public final int getAge() {
return this._age;
}
@NotNull
public final Function0 getPrintAge() {
return this.printAge;
}
}