iOS - General discussion on architectural patterns

The purpose of architectural design is to manage the complexity of development, reasonably split the logic in the project, and make it easier to maintain and manage the project, thereby saving development time and improving development efficiency.

At present, the more common centralized architectures in the actual development process mainly include MVC, MVVM, and VIPER (View Interactor Presenter Entity Router). The design ideas of these architectures are basically the same. The latter ones are all based on MVC. Controller The logic is separated out, but the degree and method are different.

First, let’s briefly introduce MVC, which is a design method officially recommended by Apple, mainly including Model, View, and Controller:

Model:封装的数据模型,用来保存数据;
View:视图控件,用来展示视图;
Controller:主要是视图控制器,用来处理用户事件,更新视图和数据等逻辑;

MVC structure hierarchy diagram

The design idea is to reduce the coupling lines between View and Mode. View only contains views, and Model only involves data. Controller is used to control the logical relationship between the two. There is no direct connection between View and Model. However, as the complexity of the project gradually increases, this design pattern exposes problems such as bloated Controller code, inconvenient management, and increased maintenance costs.

In order to reduce the burden on the Controller, the logic of integrating the View display data was extracted and encapsulated into a ViewModel. This led to the MVVM design method, which mainly includes three parts:

Model:此处的Model与MVC中的Model相同,是封装的数据模型;
View:此处的View是指的的View和ViewController,用于展示视图和接收用户事件等;
ViewModel:视图模型有直接用于展示的接口和数据输入接口,其主要作用是,将初始数据处理成为直接用于视图展示的Model;

In MVVM, two-way binding (Data and user action binding) is used to synchronize the status of View and ViewModel. Changes in View are automatically reflected in the bound ViewModel, and vice versa. The design idea is to extract the data processing logic responsible for displaying the view from the Controller based on MVP to form a ViewModel.

The logical relationship between the three elements of MVVM is:

MVVM structure hierarchy diagram

1. Model data is provided to ViewModel, processed by ViewModel and converted into data that can be directly displayed by View, and then the status of ViewMode is updated; 2. When the View
layer receives user events or status change events, the ViewModel bound to the View can also be updated synchronously , and then ViewModel will update the corresponding local Model data.

The advantages of MVVM are:
1. Low coupling, which reduces the coupling between view logic processing and other business logic;
2. Reusability, a common view logic is encapsulated into a ViewModel, and many Views can reuse this view logic. ;
3. Convenient for view unit testing. Since the view logic is split, when testing the corresponding view or style, you only need to change the view logic in the ViewModel, and it has nothing to do with other business logic;

As the complexity of the project system increases, more detailed requirements need to be abstracted and processed separately to facilitate maintenance. The user interaction logic and router responsibilities in the Controller are divided and scheduled by the coordinator (Presenter), so VIPER ( View Interactor Presenter Entity Router) architecture.

VIPER architecture design is mainly divided into five parts:

1、View:指View与ViewController,包含UI层面的业务逻辑以及交互层面的接口调用和转发; 
2、Interacter:交互器,包括用户交互动作和关于数据和网络请求的业务逻辑,例如创建一个实体(数据),或者从服务器中获取一些数据。
3、Presenter:统一调用各个部分之间的协议接口,例如将Interacter中请求来的数据传递给View层,将用户交互事件传递给路由层等;
4、Entity:实体模型,普通的数据对象;
5、Router:用于各个页面层次之间的联系绑定,页面视图跳转链接等;

VIPER architecture hierarchy diagram

The main design idea is to extract business logic from the Controller in MVC.

The VIPER architecture abstracts logic such as View, Interactor, and Router. When using it, first encapsulate different logical abstractions into corresponding protocols, and then use the protocols in the view controller to hand over different logical processing to the Presenter object that complies with all protocols and binds their corresponding relationships for unified processing. Among them, Router logic has been encapsulated by many open source libraries, and it is more convenient to call, such as JLRoutes , HHRouter , MGJRouter , etc.

Among the above design patterns, no one is better than the other. Only the one that meets the actual situation of your project is optimal. It should be noted that the above-mentioned architectural design patterns are all design ideas worth learning from. Copying a certain design pattern will not solve the problems in our projects in most cases. To keep the project in a good state of maintenance, it is necessary to promptly summarize and abstract the corresponding business components during the normal development process, and slim down the project to improve the reusability and scalability of the project. When defects in structural design are discovered, adjustment steps must be planned promptly to avoid "tar pits".

Guess you like

Origin blog.csdn.net/ID314846818/article/details/76849629