Software Engineering: Model View Controller

Tiempo de lectura: 4 minutos

There are various Model-View-Controller (MVC) architecture models that have been developed over time to suit different types of applications and development contexts.

  1. Classic MVC:

<

ul>

  • Model: Representshtml
    Copy code
    There are several Model-View-Controller (MVC) architecture models that have been developed over time to suit different types of applications and development contexts.

    1. Classic MVC:
    • Model: Represents the application’s data and business logic. It is responsible for managing the application’s state and performing data-related operations.
    • View: Responsible for presenting data to the user. It displays the user interface and communicates user actions to the controller.
    • Controller: Acts as an intermediary between the model and the view. It handles user interactions, updates the model based on user actions, and updates the view to reflect changes in the model.

    Example:

    Model:

    public class UserModel {
        private String username;
        private String password;
        
        // Getters and setters
    }

    View:

    public class UserView {
        public void printUserDetails(String username, String password) {
            System.out.println("Username: " + username);
            System.out.println("Password: " + password);
        }
    }

    Controller:

    public class UserController {
        private UserModel model;
        private UserView view;
        
        public UserController(UserModel model, UserView view) {
            this.model = model;
            this.view = view;
        }
        
        public void updateView() {
            view.printUserDetails(model.getUsername(), model.getPassword());
        }
    }
    1. Web-based MVC:
    • Model: Similar to classic MVC, it represents the data and business logic of the web application. It may include server-side logic and database interaction.
    • View: In the web context, the view represents the user interface displayed in the browser. It is usually created using web technologies like HTML, CSS, and JavaScript.
    • Controller: The controller in web MVC handles HTTP requests from the client and decides how to respond to those requests. It typically routes requests to the appropriate business logic and renders the appropriate view.

    Example:

    • Model: Similar to the Classic MVC example, but may include additional server-side logic and database access.
    • View: An HTML page that displays user details.
    • Controller: A servlet controller in Java that handles HTTP requests and decides what action to take based on the request.
    1. Dual-View MVC (MVC 2):
    • Model: As in other MVC models, it represents the data and business logic. It may include server-side logic and database interaction.
    • View: In dual-view MVC, there are two views: a presentation view that shows the user interface and a control view that shows user controls. The control view handles user interactions, while the presentation view displays the results of those interactions.
    • Controller: The controller in this model handles user interactions and coordinates between the control view and the presentation view. It is responsible for updating the model based on user actions and coordinating the update of both views.

    Example:

    Model: Similar to the Classic MVC example, but may include more logic to handle multiple views.

    Presentation View:

    public class PresentationView {
        public void showUserDetails(String username, String password) {
            System.out.println("User Details:");
            System.out.println("Username: " + username);
            System.out.println("Password: " + password);
        }
    }

    Control View:

    public class ControlView {
        public void showControls() {
            System.out.println("Edit User");
            System.out.println("Delete User");
        }
    }

    Controller:

    public class DualViewController {
        private UserModel model;
        private PresentationView presentationView;
        private ControlView controlView;
        
        public DualViewController(UserModel model, PresentationView presentationView, ControlView controlView) {
            this.model = model;
            this.presentationView = presentationView;
            this.controlView = controlView;
        }
        
        public void updatePresentationView() {
            presentationView.showUserDetails(model.getUsername(), model.getPassword());
        }
        
        public void updateControlView() {
            controlView.showControls();
        }
    }
    1. MVC in component-based or microservices architectures:
    • In component-based architectures, the model, view, and controller can be distributed across different components or services.
    • For example, in a microservices-based application, each microservice can have its own model, view, and controller, and communication between them occurs through well-defined interfaces.
    • This allows for greater scalability and modularity, as each component can be developed, tested, and deployed independently.

    Example:

    In a microservices-based architecture, each microservice might have its own implementation of MVC. For example, a user management microservice could have its own models, views, and controllers to handle user operations. Communication between these components could be done through a RESTful interface or via messages in a message queue, depending on the specific architecture used.

Leave a Comment