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.
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:
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:
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:
MVVM abstracts UI representation, focusing model operations in the ViewModel for higher cohesion and effective data binding.
Summary Table
| Design Pattern | Model Responsibilities | Where Model Operations Reside |
| MVC | Handles CRUD, validations, business logic | Primarily in the Model (some in Controller if needed) |
| MVP | Similar to MVC, centering on business logic | Mostly in Model, coordinated through Presenter |
| MVVM | Maintains a focus on data logic | Heavily 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
- Where is Apache Kafka placed in the PACELC-Theorem
- Where is the error log file destination for Zookeeper distributed with Kafka?
- Where to set parameters min.insync.replicas and acks in Java?
- Which Android IPC model to choose
- Where does Microsoft.Practices.ServiceLocation come from?
- Where to put Bean in Spring Boot?
- Which are the Data Driven Consensus Algorithms implemented in Blockchain Protocols
- Which cloud based, scalable web service is best for DDOS prevention?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.