00. Common Architectural Design Patterns

It will take about 3 minutes to finish reading this article.

Here is a summary of all the possible reasons for publishing failure in Hexo.

1. MVC(Model-View-Controller)

Model: Responsible for the data and business logic of the application.

View: Responsible for displaying the user interface and receiving user input.

Controller: handles user input, updates the model, and updates the view when the model changes.

1
2
3
4
5
6
      Model
|
+----+----+
| |
V V
View Controller

Problem-solving:

Separation of concerns: Divides the application into components with distinct responsibilities.
Code organization: Improves maintainability and scalability by organizing code into three interconnected components.

Scenes:

(1) UIKit Applications:

MVC is widely used in UIKit-based iOS applications. Views are responsible for presenting the user interface, controllers handle user input and application logic, and models manage data and business logic.

(2) Table View Controllers:

In applications with lists or tables of data, such as contact lists or news feeds, MVC is commonly employed. The table view itself is the view, the view controller manages the table view and user interactions, and the model holds the data.

(3) Form-Based Apps:

Applications that involve forms and input fields, like login screens or user profile editing, can benefit from MVC. Views represent the form elements, controllers manage user input, and models handle data validation and storage.

Best suited for applications with a rich user interface.
Commonly used in web development frameworks like Ruby on Rails, Django, and ASP.NET.

Pros and Cons:

Pros:

Separation of concerns enhances code maintainability.
Reusable components make code more modular.
Well-established pattern with a long history of usage.

Cons:

Can lead to complex and tightly coupled code.
Controller logic may become bloated over time.

2. MVP (Model-View-Presenter)

Model: Represents the application’s data and business logic.

View: Displays the data to the user and handles user input.

Presenter: Acts as an intermediary between the Model and the View, handling user input and updating the Model.

1
2
3
4
5
6
     Model
|
+----+----+
| |
V V
View <---- Presenter

Problem-solving:

Separation of concerns: Similar to MVC, it separates the application into distinct components.
Testability: Easier to unit test the Presenter due to its isolation from the View.

Scenes:

(1) iOS Forms and Input Processing:

MVP can be beneficial when dealing with complex forms or input processing. The view manages the UI, the presenter handles user input and validation, and the model deals with data storage.

(2) Interactive UI Elements:

When UI elements require more interactivity and logic, such as dynamically updating views based on user input, MVP can provide a clear separation of concerns. The presenter acts as an intermediary, updating the view based on changes in the model.

(3) Unit Testing View Logic:

MVP allows for easier unit testing of the presenter, as it encapsulates the logic that would traditionally be in the controller. This is particularly useful in scenarios where extensive UI testing is not practical.

Well-suited for scenarios where the logic related to user input is complex.
Often used in desktop and web applications.

Pros and Cons:

Pros:

Improved testability due to the separation of concerns.
Easier to maintain and understand than some variations of MVC.

Cons:
Can still result in tight coupling between the View and Presenter.
The Presenter may become a “God” class if not managed carefully.

3. MVVM(Model-View-ViewModel)

Model: Responsible for the data and business logic of the application.

View: Responsible for displaying the user interface and receiving user input.

ViewModel: Abstracts the state and behavior of the view and is responsible for processing the data required by the view.

1
2
3
4
5
6
     Model
|
+----+----+
| |
V V
View <---- ViewModel

Problem-solving:

Declarative UI: Enhances the separation of concerns by allowing a more declarative definition of the user interface.
Testability: Promotes easier unit testing of the ViewModel.

Scenes:

(1) Data-Driven Applications:

MVVM is well-suited for data-driven applications where the UI elements need to be dynamically updated based on changes in the underlying data. This is common in applications displaying real-time data, such as financial apps or news readers.

(2) Reactive Programming:

MVVM pairs well with reactive programming frameworks like RxSwift. The ViewModel can be used to represent and transform data streams, providing a seamless way to update the UI based on changes in the model.

(3) Complex UIs with Binding:

Applications with complex user interfaces, especially those involving multiple interconnected elements, benefit from MVVM. The binding mechanism simplifies the synchronization of the view with the ViewModel, reducing boilerplate code.

Particularly effective in data-binding frameworks (e.g., Angular, React, Vue.js).
Commonly used in modern mobile and web applications.

Pros and Cons:

Pros:

Improved separation of concerns with a focus on data binding.
Easier to unit test due to the ViewModel’s isolation from the View.
Well-suited for modern, data-driven applications.

Cons:

Learning curve for developers unfamiliar with data-binding concepts.
Overuse of two-way data binding can lead to performance issues.