Facebook Twitter Instagram
    DeepCrazyWorld
    Facebook Twitter Instagram Pinterest YouTube
    • FLUTTER
      • FLUTTER APP
        • QRCode
        • Quiz App
        • Chat GPT
        • PDF App
        • News App
        • Fitness App
        • Weather App
        • BMI Calculator
        • GAME APP
        • Ecommerce App
        • wallpaper App
        • Finance app
        • Chat App
        • Wallet App
        • Taxi App
        • Quran app
        • Music player app
      • FLUTTER UI
        • Splash Screen
        • Onboarding Screen
        • Login Screen
        • Card Design
        • Drawer
    • PROJECT
      • Android Projects
      • College Projects
      • FLUTTER APP
      • Project Ideas
      • PHP Projects
      • Python Projects
    • SOURCE CODE
    • ANDROID
      • ANDROID APP
      • GAME APP
      • ANDROID STUDIO
    • MCQ
      • AKTU MCQ
        • RPA MCQ
        • COA MCQ
        • HPC MCQ
        • SPM MCQ
        • Renewable Energy All MCQ
        • Data Compression MCQ
        • Data Structure MCQ
        • Digital Image Processing MCQ
        • Software Engineering MCQ
        • Machine Learning MCQ
        • Artificial Intelligence MCQ
      • D PHARMA MCQ
        • Pharmaceutics – I MCQ
        • Pharmacognosy MCQ
        • Pharmaceutical Chemistry MCQ
        • Biochemistry and Clinical Pathology MCQ
        • Human Anatomy and Physiology MCQ
        • Heath Education and Community Pharmacy MCQ
    • INTERVIEW QUESTIONS
      • Flutter Interview Questions
      • INTERVIEW QUESTIONS
      • Python Interview Questions
      • Coding ninjas solution
    • MORE
      • WORDPRESS
        • SEO
        • TOP 10 WORDPRESS THEME
      • PRODUCTIVITY
      • Program
      • QUOTES
    DeepCrazyWorld
    Home»Web Developer»Model View Controller Pattern – MVC Architecture & Frameworks(updated)
    Web Developer

    Model View Controller Pattern – MVC Architecture & Frameworks(updated)

    DeepikaBy DeepikaDecember 3, 2021Updated:April 17, 2023No Comments6 Mins Read

    Model View Controller Pattern – The MVC architecture pattern turns complex application development into a much more manageable process. It allows several developers to simultaneously work on the application.

    Table of Contents

    Toggle
    • What is MVC?
    • Why Should You Use MVC?
    • How to Use MVC
    • Now let’s divide into these three components that make up the MVC architecture pattern.
    • Model (data)
    • Views (UI)
    • carListView
    • CarView
    • Controller (Brain)
    • MVC Frameworks
    • Conclusion
    • Additional Reading
        • you can read more articles like this here.
    • READ MORE

    What is MVC?

    MVC stands for model-view-controller. Here’s what each of those components mean:

    • Model: The backend that contains all the data logic
    • View: The frontend or graphical user interface (GUI)
    • Controller: The brains of the application that controls how data is displayed
    <img decoding=

    The concept of MVCs was first introduced by Trygve Reenskaug, who proposed it as a way to develop desktop application GUIs.

    Today the MVC pattern is used for modern web applications because it allows the application to be scalable, maintainable, and easy to expand.

    Why Should You Use MVC?

    Three words: separation of concerns, or SoC for short.

    The MVC pattern helps you break up the frontend and backend code into separate components. This way, it’s much easier to manage and make changes to either side without them interfering with each other.

    But this is easier said than done, especially when several developers need to update, modify, or debug a full-blown application simultaneously.

    How to Use MVC

    To better illustrate the MVC pattern, I’ve included a web application that shows how these concepts all work.

    My Car Clicker application is a variation of a well-known Cat Clicker app.

    Here are some of the major differences in my app:

    1. No cats, only muscle cars images (sorry cat lovers!)
    2. Multiple car models are listed
    3. There are multiple click counters
    4. It only displays the selected car
    <img loading=

    Now let’s divide into these three components that make up the MVC architecture pattern.

    Model (data)

    The model’s job is to simply manage the data. Whether the data is from a database, API, or a JSON object, the model is responsible for managing it.

    In the Car Clicker application, the model object contains an array of car objects with all the information (data) needed for the app.

    It also manages the current car being displayed with a variable that’s initially set to null.

    const model = {
        currentCar: null,
        cars: [
            {
                clickCount: 0,
                name: 'Coupe Maserati',
                imgSrc: 'img/black-convertible-coupe.jpg',
            },
            {
                clickCount: 0,
                name: 'Camaro SS 1LE',
                imgSrc: 'img/chevrolet-camaro.jpg',
            },
            {
                clickCount: 0,
                name: 'Dodger Charger 1970',
                imgSrc: 'img/dodge-charger.jpg',
            },
            {
                clickCount: 0,
                name: 'Ford Mustang 1966',
                imgSrc: 'img/ford-mustang.jpg',
            },
            {
                clickCount: 0,
                name: '190 SL Roadster 1962',
                imgSrc: 'img/mercedes-benz.jpg',
            },
        ],
    };

    Views (UI)

    The view’s job is to decide what the user will see on their screen, and how.

    The Car Clicker app has two views: carListView and CarView.

    Both views have two critical functions that define what each view wants to initialize and render.

    These functions are where the app decides what the user will see and how.

    carListView

    const carListView = {
        init() {
            // store the DOM element for easy access later
            this.carListElem = document.getElementById('car-list');
    
            // render this view (update the DOM elements with the right values)
            this.render();
        },
    
        render() {
            let car;
            let elem;
            let i;
            // get the cars to be render from the controller
            const cars = controller.getCars();
    
            // to make sure the list is empty before rendering
            this.carListElem.innerHTML = '';
    
            // loop over the cars array
            for(let i = 0; i < cars.length; i++) {
                // this is the car we've currently looping over
                car = cars[i];
    
                // make a new car list item and set its text
                elem = document.createElement('li');
                elem.className = 'list-group-item d-flex justify-content-between lh-condensed';
                elem.style.cursor = 'pointer';
                elem.textContent = car.name;
                elem.addEventListener(
                    'click',
                    (function(carCopy) {
                        return function() {
                            controller.setCurrentCar(carCopy);
                            carView.render();
                        };
                    })(car)
                );
                // finally, add the element to the list
                this.carListElem.appendChild(elem);
            }
        },
    };

    CarView

    const carView = {
        init() {
            // store pointers to the DOM elements for easy access later
            this.carElem = document.getElementById('car');
            this.carNameElem = document.getElementById('car-name');
            this.carImageElem = document.getElementById('car-img');
            this.countElem = document.getElementById('car-count');
            this.elCount = document.getElementById('elCount');
    
    
            // on click, increment the current car's counter
            this.carImageElem.addEventListener('click', this.handleClick);
    
            // render this view (update the DOM elements with the right values)
            this.render();
        },
    
        handleClick() {
        	return controller.incrementCounter();
        },
    
        render() {
            // update the DOM elements with values from the current car
            const currentCar = controller.getCurrentCar();
            this.countElem.textContent = currentCar.clickCount;
            this.carNameElem.textContent = currentCar.name;
            this.carImageElem.src = currentCar.imgSrc;
            this.carImageElem.style.cursor = 'pointer';
        },
    };

    Controller (Brain)

    The controller’s responsibility is to pull, modify, and provide data to the user. Essentially, the controller is the link between the view and model.

    Through getter and setter functions, the controller pulls data from the model and initializes the views.

    If there are any updates from the views, it modifies the data with a setter function.

    const controller = {
        init() {
            // set the current car to the first one in the list
            model.currentCar = model.cars[0];
    
            // tell the views to initialize
            carListView.init();
            carView.init();
        },
    
        getCurrentCar() {
        	return model.currentCar;
        },
    
        getCars() {
        	return model.cars;
        },
    
        // set the currently selected car to the object that's passed in
        setCurrentCar(car) {
        	model.currentCar = car;
        },
    
        // increment the counter for the currently-selected car
        incrementCounter() {
            model.currentCar.clickCount++;
            carView.render();
        },
    };
    
    // Let's goooo!
    controller.init();

    MVC Frameworks

    JavaScript has grown in popularity, and it’s taken over the backend in recent years. More and more full-blown JavaScript applications have opted for the MVC architecture pattern in one way or another.

    Frameworks come and go, but what has been constant are the concepts borrowed from the MVC architecture pattern.

    Some of the early frameworks that applied these concepts were KnockoutJS, Django, and Ruby on Rails.

    Conclusion

    The most attractive concept of the MVC pattern is separation of concerns.

    Modern web applications are very complex, and making a change can sometimes be a big headache.

    Managing the frontend and backend in smaller, separate components allows for the application to be scalable, maintainable, and easy to expand.

    I hope you found value from this article.

    Additional Reading

    • SEO Practices Everyone Should Follow SEO Rules
    • Complete Top SEO Checklist
    • Yoast Seo Premium 15.2 Nulled – WordPress SEO Plugin
    • Top 50+ SEO Interview Questions
    • What is a Backlink? How to Get More Backlinks
    • TCS INTERVIEW QUESTIONS – CLICKE HERE
    • Top 20 Interview Program Questions
    • Android Projects with Source Code
    • Python Project With Source Code
    • Python Projects Ideas
    • Machine Learning MCQ Questions
    • Highest Paying Earning Website 
    • School Database Management System 
    • Top 20 Company Interview Questions

    you can read more articles like this here.

    READ MORE

    If you found this post useful, don’t forget to share this with your friends, and if you have any query feel free to comment it in the comment section.

    Thank you 🙂 Keep Learning !

    Share. Facebook Twitter LinkedIn WhatsApp Telegram Pinterest Reddit Email
    Previous ArticleFreelance Developer Guide-The Different Ways to Charge for Website
    Next Article Top 20 Freelance job & Part Time Find Work online 2023 (updated)

    Related Posts

    Top 20 Amazing Websites जिनके बारे में आपको जानना चाहिए 2023

    Web Developer 4 Mins Read

    Top 5 Beginner’s Guide: How To Learn Web Design At Home 2023

    Web Developer 12 Mins Read

    Unable to load class ‘javax.xml.bind.JAXBException’

    Web Developer 3 Mins Read

    UGC NET 2021: What is UGC NET Exam? Exam Details and Eligibility Criteria

    Web Developer 8 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • Implementing a Dynamic FAQ Screen UI in Flutter Using ExpansionTile March 29, 2025
    • Creating an Instruction UI Screen in Flutter Application March 29, 2025
    • Animated Backgrounds in Flutter: A Complete Guide March 15, 2025
    • How to make Diary App using flutter stepwise using getx August 31, 2024
    • How to Create Music Player UI screen with fully functional in flutter August 30, 2024
    • How to make ListView Builder Ui in flutter with Source Code August 29, 2024
    • Create a TabBar View in flutter with fully functional stepwise August 28, 2024
    • How to create TabBar view in flutter with source code step wise August 27, 2024
    • How to make Heart rate measure app with Flutter stepwise August 26, 2024
    • How to make ChatGpt App in flutter with source code Stepwise August 25, 2024
    Facebook Twitter Instagram Pinterest YouTube
    • About
    • Contact
    • Disclaimer
    • Privacy Policy
    Copyright by DeepCrazyWorld © 2025

    Type above and press Enter to search. Press Esc to cancel.