The MVC design pattern
The Model View
Controller Pattern is mainly used for separating the presentation logic from
the business model.
This Pattern was first
introduced in the Smalltalk way back in the 1970s, since then it has played an
important role in most UI Frameworks.
Why MVC?
In Object oriented
program, UI, database and other support code often gets written directly into
the business object or the domain code (like business rules, policies, database
manipulation etc.) and the UI code are coupled together in a single object.
This practice has been
followed for one simple reason, it is easier to make things work in the short
run.
This is a reasonable
approach for quick prototyping and if the program is small in size and there is
no plan for further enhancement.
The problem arises when
the program grows in size, it becomes more difficult to add/extend new
features,
the readability of the
code is lost, the developer ends up writing duplicate code and it’s not easy to
test the domain logic(in some cases impossible). UI logic tends to change more
frequently than that of domain, for example existing views are shuffled around,
which cause changes to the domain code as well. Complexity buries you quickly.
The Program starts screaming for Separation!
There are many ways to
achieve this separation of UI and the Domain code. The MVC is one of the widely
used pattern to solve this problem.
How It Works ?
This pattern has below
three key elements,
·
Model: The model is responsible to manage the data and behavior of the
business domain. This is the place where all the business logic lives. It
responds to the request for the information about its state (usually from the
view) as well as to the instruction to change the state (usually from the
controller).
Also it is responsible to notify if there are any changes to its
state, this is done by the model with the help of Observer Pattern. Model
shouldn’t have any knowledge about controller and view.
·
View: The view is where all the presentation logic goes, like how to
present the information received from the model.
·
Controller: It acts as a glue between the view and the model. In most cases
it abstracts away the “Model” from the “View”. Controller shouldn’t have any
dependency on view.
In above diagram, notice
that the model is notifying the view whenever it changes its state.
Doesn’t it look wrong? One
of the motivation to use the MVC pattern is to solve this coupling problem
between “View” and “Model”. If the model had to notify the view of changes, you
would reintroduce the dependency you were looking to avoid. Here comes the
savior the “Observer Pattern”, it has the mechanism to notify the other objects
of sate change without introducing the direct dependency on them.
Below figure shows how
it all works together,
Creating a program that
can handle very complex tasks calls for separation of concerns.
Nonvisual objects are
easier to test than that of visual ones. Separating Presentation logic from
business objects allows you to test the business logic easily.
Good explanation
ReplyDelete