Author image

ぺんすけブログ

リモートワークと子育てとTechな日常

Cover image

MVCを理解する【デザインパターン】

そもそもModelは曖昧なもの

Modelって何?

そもそもMVCはなんだったのか。モデルって何?ということがわかりやすく解説されています。

サービスクラス、その前に

PDS: PresentationDomainSeparation

PresentationDomainSeparation(PDS) は上述のスライドで言及されているMVCの前の設計原則の考え方です。

設計原則としてプレゼンテーション(ユーザーインターフェース)と残りの部分(ドメイン)をわけましょうというものです。

いわば見た目それ以外をわけたいんですね。MVCも分けるために間をとりもつControllerがあるという意味でこの原則に則っているのかなとぼくは思います。

(太字は筆者によるものです。)

One of the most useful design principles that I've found and followed is that of keeping a good separation between the presentation aspects of a program (the user interface) and the rest of the functionality

PresentationDomainSeparation

Gof本に記載されているMVC

下記は通称Gof本からの引用です。

MVC consists of three kinds of objects. The Model is the application object, the View is its screen presentation, and the Controller defines the way the user interface reacts to user input.

見やすくまとめるとこうですね。

Model: the application object
Controller: defines the way the user interface reacts to user input.
View: its screen presentation

PresentationDomainSeparationにおいて

  • View→Presentation
  • Model→Domain

に該当するとして残りのControllerが、「ユーザーの入力からどういうリアクションをするかを決めるもの」という風に定義されていますね。

MVCについてまとめ

こういう風に見ていくと、Modelがそもそも曖昧なものだということが浮き彫りになってきたかなと思います。これが理解しようと思ってもなかなか理解を難しくしている原因かなと思います。

「ModelはDB周りの処理じゃない、ビジネスロジックだ」とか言われてもぱっとイメージできないですよね。

「見た目(presentation)とそれ以外(domain)を分けたかったのでいわゆるModelは見た目(view)以外のもの。それをつなぎこむためにControllerがあるよ」 っていうふわっとした説明の方がぼくにはしっくりきます。

参考

通称Gof本
Design Patterns: Elements of Reusable Object-Oriented Software