วันศุกร์ที่ 28 สิงหาคม พ.ศ. 2569

MVP vs MVC

 The primary difference between MVP and MVC is how the View interacts with the Model and Middle Layer. In MVC, the View can observe the Model directly to update itself, whereas in MVP, the View is completely passive and can only communicate with the Model through the Presenter.


🏛️ Architecture and Data Flow
Model-View-Controller (MVC)
  • The Flow: The User interacts with the View → View triggers the Controller → Controller updates the Model → Model notifies the View directly to change the screen.
  • Coupling: The View and the Model are aware of each other, creating tighter coupling.
  • Relationship: One Controller can manage multiple different Views.
Model-View-Presenter (MVP)
  • The Flow: The User interacts with the View → View passes the event to the Presenter → Presenter updates the Model → Model returns data to the Presenter → Presenter formats data and manually updates the View.
  • Coupling: Isolated. The View and Model never talk to each other.
  • Relationship: Typically a strict 1:1 mapping between a View and its Presenter.

📊 Side-by-Side Comparison
FeatureModel-View-Controller (MVC)Model-View-Presenter (MVP)
View Passive or Active?Active: Directly listens to data changes from the Model.Passive: Strictly waits for instructions from the Presenter.
Middle Layer KnowledgeController selects the View but doesn't manage its UI components closely.Presenter commands the View directly via a predefined interface.
Testing UI LogicDifficult: Highly tied to the platform UI lifecycles and components.Easy: Presenter logic can be tested entirely with mock interfaces.
Component CouplingHigh (View and Model are connected).Low (View and Model are completely separated).
Primary Use CasesTraditional web frameworks (e.g., ASP.NET Core, Ruby on Rails).Legacy or highly modular Android/iOS apps, complex desktop UIs.

🎭 An Everyday Analogy: The Restaurant
  • The MVC Restaurant: You (User) give an order to the Waiter (Controller). The Waiter gives it to the Kitchen (Model). When the food is ready, the Kitchen staff calls out your name directly or slides the plate to your table (Model updates View directly).
  • The MVP Restaurant: You (User) tell the Waiter (Presenter). The Waiter talks to the Kitchen (Model). The Kitchen hands the raw plate back to the Waiter. The Waiter garnishes it, ensures it looks exactly right for your specific table layout, and places it down in front of you (Presenter manually forces View updates).