3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

Androidアプリを書いていた時に調べ、迷い、結局それが何たるかを理解するのは難しかったMVVMについて理解できた範囲で調べたことを書いていきます。

MVVMとは

MVVM(Model-View-ViewModel)は、ソフトウェアアーキテクチャのパターンで、特にGUIアプリケーションの開発において使われることが多いです。MVVMはModel、ViewModel、Viewの3つの主要なコンポーネントで構成されています。
MVVMの主な目的は、ビジネスロジック(Model)とUI(View)の分離を強化し、それらをよりテスト可能で保守しやすい状態に保つことです。また、データバインディングを活用して、ViewModel と View の間でデータの同期を容易にします。

image.png

画像引用

原典

MVVM(Model-View-ViewModel)パターンの考案者は、マイクロソフトのジョン・ガスパー(John Gossman)です。
この方は、WPF(Windows Presentation Foundation)の開発チームの一員として、MVVMパターンを推奨しました。

Model

フィールドとそれを取得するための処理をかく。データの全量。

どちらかというと、UIの制約を意識しない処理ロジックはここに全て書く。ちなみに、UIの制約を意識しないのであってUI自体を全く意識しないわけではない。

ViewModel

ViewModelはUIの制約を吸収するための処理ロジックや加工処理などを書く。そのためViewが決まればViewModelが決まる、というイメージが正しいらしい。

その値や処理の結果をViewModelクラスのインスタンスにセットし、DatabindingによりViewの値が自動的に変更される(DatabindingのほかにもCommandやビヘイビア等の方法もある)。

また、Modelにて永続的に管理する必要もないような一時的な値(いまViewがDrag状態であるか否かなど)はViewModelのインスタンスが持つ。

View

画面を描画する。
ViewModelのインスタンスのフィールドとViewが参照する値がDatabinding等により同期されることにより、View-ViewModel間が疎結合になる。

難しいところ

ViewModelの定義が一番難しいと感じました。
原典には以下のように記載してあり、ここが参考になりそうです(太字は私がつけました)。

In practice however, only a small subset of application UI can be data bound directly to the Model, especially if the Model is a pre-existing class or data schema over which the application developer has no control. The Model is very likely to have a data types that cannot be mapped directly to controls. The UI may want to perform complex operations that must be implemented in code which doesn't make sense in our strict definition of the View but are too specific to be included in the Model (or didn't come with the pre-existing model). Finally we need a place to put view state such as selection or modes.

The ViewModel is responsible for these tasks. The term means "Model of a View", and can be thought of as abstraction of the view, but it also provides a specialization of the Model that the View can use for data-binding. In this latter role the ViewModel contains data-transformers that convert Model types into View types, and it contains Commands the View can use to interact with the Model.

参考

原典
Introduction to Model/View/ViewModel pattern for building WPF apps
MVVMとは何か
MVVMにおけるViewModelとModelのプロパティ同期 - NotifyPropertyHelper 1.0.0-beta1
MVVMのModelにまつわる誤解 - the sea of fertility
【Android】2020年からの MVVM【実践】 - Qiita
RxJava + MVVM パターンで作るストップウォッチアプリ - Qiita
Android Architecture Components 初級 ( MVVM + LiveData + Coroutines 編 ) - Qiita
Android ViewModel が不要である理由
【Android】初心者が意識すべきMVVMでの開発ルール
Android MVVM + LiveData + DataBinding(初心者向け) - Qiita
【Android】MVVMについて
【Android】Android の Data Binding でできること(基本編) - Tumbling Dice
監視可能なデータ オブジェクトの使用  |  Android デベロッパー  |  Android Developers
DataBindingで値を自動で画面へ反映する - Qiita
DataBindingチュートリアル第3回 – BindingAdapter(バインディングアダプター)|mizutory|note
DataBindingチュートリアル第5回 – データに表現させる|mizutory|note
GitHub - sdkei/Android_MVVM_Mediator_Login: 書籍「増補改訂版 Java言語で学ぶ デザインパターン入門」の16章 Mediator に出てくるログインダイアログを、Android の MVVM で実装する。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?