Chapter 3 ■ IntroduCtIon to MVC
A Separation of Concerns
Great! We have some background on the three core components of MVC and how they relate to one another. Though,
at this stage, it may not be entirely clear why we should be using it. Now let’s take a look at the underlying purpose of
this pattern and what sort of problems it solves.
As is clear from the definitions above, the controller is actually keeping the model and the view separated—one
has no direct knowledge of the other. This is a fairly common design in software engineering, and the term used to
describe it is decoupling.
When you organize your applications in this manner, it promotes a principle known as Separation of Concerns.
Software that has been built around this principle tends to have distinct parts, each of which looks after a particular
concern. MVC is a great way of achieving this Separation of Concerns, and before I end this chapter, we will take a
quick first look at how AngularJS helps you build your applications in this manner.
I have talked a little bit about MVC and Separation of Concerns, but how do these ideas translate into benefits
for programmers and end users? Why should we take the extra time and effort it takes to build our applications in this
manner?
Why MVC Matters
A classic benefit of MVC is that you can, with relative ease, add a new format to your application. That is, you can
start off with a standard HTML-based set of views and then later add a new set of views supporting a totally different
format, such as Silverlight or a native mobile front end. Trying to achieve something like this when your application
has been poorly designed would be a nightmare. Take it from someone who has tried it both with and without an
MVC-style architecture—the difference in the effort required is huge!
The benefit stated previously exists because, through MVC, we apply the principle of Separation of Concerns. The
view is in no way exclusively tied to the model, so it is far easier to treat it as a distinct component that we can swap
out for another (or, as is quite common, compliment with another).
There are also benefits with regard to the methodologies and processes you (and your team) can use. For
example, Test-Driven Development (TDD) is very popular at present, and it leads to applications that are much
easier to test and continue to test as the application matures. Without achieving a solid Separation of Concerns, it can
become much trickier to set up good tests.
Figure 3-1. The MVC lines of communication
Controller
You can think of the controller as the intermediary for the view and the model. Examine Figure 3-1. You can see that
the lines of communication correspond to this idea.