잡담

MVC Pattern

tomato13 2013. 12. 23. 22:21

http://www.phpwact.org/pattern/model_view_controller

http://stackoverflow.com/questions/5966905/which-mvc-diagram-is-correct-web-app



The primary goal of MVC is to isolate UI changes and prevent them from requiring changes to the Domain Logic of the application.

( The primary reason for this division is that the user interface and Domain Logic have different drivers for change and different rates of change.)


The goal of MVC is to make the model independent of the view and controller which together form the user interface of the application.



Passive Model

With a passive model, the objects used in the model are completely unaware of being used in the MVC triad. The controller notifies the view when it executes an operation on the model that will require the view to be updated.


Active Model

In the active model, model classes define a change notification mechanism, typically using the Observer pattern. This allows unrelated view and controller components to be notified when the model has changed. Because these components register themselves with the model and the model has no knowledge of any specific view or controller, this does not break the independence of the model.


정리하면,

MVC패턴의 핵심은 Model과 View를 분리하는 것이다. (Model과 View는 성격이 달라 서로 다른 maintenance plan을 가지게 되므로 각기 분리되는 것이 좋다.)

특히, Model의 경우에는 Controller와 View로부터 완전히 분리시켜서 하나의 독립된 컴포넌트화시키게 된다.


Model은 passive model과 active model로 분리되는데 passive model의 경우 model이 notify기능을 가지지 않게 된다. Active모델의 경우에는 observer pattern방식에 의해서 notify기능을 가지게 된다.


즉, MVC가 왜 좋을까? 이유인즉, Model을 완전하게 분리시켜서 View가 변할 경우 이에 대한 대응을 독립적으로 하게 하기 위함이다. 

(참고로 passive모델의 경우 view가 model에 직접 접근하여 data를 가져올 수도 있고 controller를 통해서 데이터를 제공받을 수도 있다. 어떤 그림은 전자의 경우를 표현하고 어떤 경우는 후자의 경우를 표시하는데 Model이 독립적으로 운영되는것이 중요할뿐 이는 그다지 이슈가 아닌듯하다.)