DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. 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 · Opinion
Like (34)
Save
Tweet
Share
28.77K 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

  • Solving the Kubernetes Security Puzzle
  • Building Microservice in Golang
  • Multi-Cloud Integration
  • Stop Using Spring Profiles Per Environment

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: