MVC vs. OOP
MVB Yegor Bugayenko smashes yet another well-accepted ''pattern'' as it violates the principles of OO. MVC frameworks, beware!
Join the DZone community and get the full member experience.Join For Free
model-view-controller (mvc) is an architectural pattern we all are well aware of. it's a de-facto standard for almost all ui and web frameworks . it is convenient and easy to use. it is simple and effective. it is a great concept... for a procedural programmer. if your software is object-oriented, you should dislike mvc as much as i do. here is why.
this is how an mvc architecture looks:
the controller is in charge , taking care of the data received from model and injecting it into view — and this is exactly the problem. the data escapes the model and becomes "naked", which is a big problem, as we agreed earlier. oop is all about encapsulation — data hiding.
mvc architecture does exactly the opposite by exposing the data and hiding behavior. the controller deals with the data directly, making decisions about its purpose and properties, while the objects, which are supposed to know everything about the data and hide it, remain anemic . that is exactly the principle any procedural architecture is built upon: the code is in charge of the data. take this c++ code, for example:
is the controller. it gets the data
from the model
and renders it via the view
. only the controller knows that the data is in miles per hour. the engine returns
without any properties. the controller simply assumed that that data is in mph. if we want to create a similar controller somewhere else, we will have to make a similar assumption again and again. that's what the "naked data" problem is about, and it leads to serious maintainability issues.
this is an object-oriented alternative to the code above (pseudo-c++):
returns speed in mph, as an integer;
; and finally,
returns the full text of the message. we can call them "model, view, and controller", but in reality they are just objects decorating each other. it's still the same entity — the speed. but it gets more complex and intelligent by being decorated.
we don't tear the concept of speed apart. the speed is the speed, no matter who works with it and where it is presented. it just gets new behavior from decorators. it grows, but never falls apart.
to summarize, controller is a pure procedural component in the mvc trio, which turns model into a passive data holder and view into a passive data renderer. the controll er , the hold er , the render er ... is it really oop ?
Published at DZone with permission of Yegor Bugayenko. See the original article here.
Opinions expressed by DZone contributors are their own.