Application Design Patterns
Model Operations
Software Architecture
Design Patterns
Model-View-Controller

Where do operations on models belong in Application Design Patterns?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Application design patterns are essential strategies utilized in software development to solve common problems within a given context efficiently. One critical question in the architecture of applications is determining where operations on models should reside. This article explores where these operations belong within various application design patterns, emphasizing technical implementations and providing relevant examples.

Application Design Patterns Overview

Design patterns are generalized solutions to recurring design problems. They help in creating a well-structured and maintainable codebase. There are numerous design patterns, but fundamentally, they often involve the separation of concerns, partitioning an application into different sections with specific roles.

Common Patterns and Model Operations

Model operations are typically associated with accessing and manipulating data. Deciding where these operations fit is crucial in maintaining code organization and ensuring scalability. Below, we'll delve into a few common design patterns and examine where model operations may reside.

Model-View-Controller (MVC)

In the MVC pattern:

  • Model: It encapsulates the application data and business logic. Operations on models, such as CRUD (Create, Read, Update, Delete) operations, validations, and data transformation, should be handled here.
  • View: It presents the data to the user. The view receives the data from the controller and can directly fetch it from the model.
  • Controller: It acts as an intermediary between the Model and the View. In some implementations, operations on models, especially those requiring coordination among multiple models, might be managed by the controller.

Example:

python
1# In a simplified Django-like MVC structure:
2class UserModel:
3    def create_user(self, data):
4        # Logic to save data to the database
5        pass
6
7class UserController:
8    def save_user(self, data):
9        model = UserModel()
10        model.create_user(data)
11
12class UserView:
13    def display_user(self, user_data):
14        print(user_data)

In this example, the UserModel handles the data operations directly, following the typical MVC separation of concerns.

Model-View-Presenter (MVP)

In the MVP pattern:

  • Model: Similar to MVC, the model is responsible for the business logic and maintaining application data.
  • View: Handles the display of the data but does not call model methods directly.
  • Presenter: Acts as a mediator that retrieves data from the model and formats it for display by the view. Complex operations on models might be orchestrated within the Presenter.

Example:

java
1public class UserPresenter {
2    private UserModel model;
3    private UserView view;
4
5    public UserPresenter(UserModel model, UserView view) {
6        this.model = model;
7        this.view = view;
8    }
9
10    public void updateUser(String data) {
11        model.save(data);
12        view.showSuccess();
13    }
14}

Here, the UserPresenter manages the interaction between the view and the model, encapsulating model operations within its methods.

Model-View-ViewModel (MVVM)

In the MVVM pattern:

  • Model: As in MVC and MVP, the model handles data logic.
  • View: Displays data and binds to properties exposed by the ViewModel.
  • ViewModel: Acts as an abstraction of the view, where operations on the model are often implemented. The ViewModel is particularly effective in supporting data binding, making it suitable for operations that update UI components reactively.

Example:

csharp
1public class UserViewModel : INotifyPropertyChanged {
2    private UserModel userModel;
3    private string username;
4
5    public string Username {
6        get { return username; }
7        set {
8            username = value;
9            OnPropertyChanged(nameof(Username));
10        }
11    }
12
13    public void LoadUser(int userId) {
14        userModel = userModel.GetUserById(userId);
15        Username = userModel.Username;
16    }
17}

MVVM abstracts UI representation, focusing model operations in the ViewModel for higher cohesion and effective data binding.

Summary Table

Design PatternModel ResponsibilitiesWhere Model Operations Reside
MVCHandles CRUD, validations, business logicPrimarily in the Model (some in Controller if needed)
MVPSimilar to MVC, centering on business logicMostly in Model, coordinated through Presenter
MVVMMaintains a focus on data logicHeavily in ViewModel for data binding compatibility

Conclusion

Choosing where to locate operations on models depends significantly on the selected design pattern and the particular needs of an application. While MVC, MVP, and MVVM all anchor model logic within the model layer, MVC may sometimes distribute it to the controller, MVP utilizes the presenter for intermediary tasks, and MVVM often places significant logic in the ViewModel for seamless UI interactions.

The flexibility of these patterns allows developers to structure applications efficiently, making robust decisions about placing model operations to optimize maintainability, readability, and performance.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.