DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > MVC vs. OOP

MVC vs. OOP

MVB Yegor Bugayenko smashes yet another well-accepted ''pattern'' as it violates the principles of OO. MVC frameworks, beware!

Yegor Bugayenko user avatar by
Yegor Bugayenko
·
Dec. 15, 16 · Web Dev Zone · Opinion
Like (34)
Save
Tweet
28.34K Views

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.

hot shots! (1991) by jim abrahams
hot shots! (1991) by jim abrahams

this is how an mvc architecture looks:

plantuml svg diagram

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:

void print_speed() { // controller
  int s = load_from_engine(); // model
  printf("the speed is %d mph", s); // view
}

the function print_speed() is the controller. it gets the data s from the model load_from_engine() and renders it via the view printf() . only the controller knows that the data is in miles per hour. the engine returns int 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++):

printf(
  new printedspeed( // view
    new formattedspeed( // controller
      new speedfromengine() // model
    )
  )
);

here, speedfromengine.speed() returns speed in mph, as an integer; formattedspeed.speed() returns "%d mph" ; and finally, printedspeed.to_str() 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 ?

Object-oriented programming Data (computing)

Published at DZone with permission of Yegor Bugayenko. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Adaptive Change Management: A DevOps Approach to Change Management
  • Data Visualization of Healthcare Expenses by Country Using Web Scraping in Python
  • Conducting Sprint Retrospective Meetings
  • Implementing One and Two Way SSL (Mutual Authentication) for MuleSoft Application

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo