swift 架构
In this article, we’ll learn how to implement a Clean VIP architecture in Swift. We’ll do so by creating a Core Data powered app that saves, deletes, and displays items in a UITableView
. It also has a details page to illustrate how we navigate and pass data between modules.
在本文中,我们将学习如何在Swift中实现Clean VIP架构。 为此,我们将创建一个支持Core Data的应用程序,该应用程序可以保存,删除和显示UITableView
项目。 它还有一个详细信息页面,以说明我们如何在模块之间导航和传递数据。
The VIP pattern was created to address some issues of the VIPER architecture. One problem was that the Presenter
(P
) component may become too large — as it’s responsible for handling UI actions and preparing data for the view. To facilitate the single responsibility principle the relationship between objects was altered. While the VIPER’s V
part means both ViewController
and a UIView
, the VIP’s “V
” means simply ViewController
. Therefore, the UIView
is a separate component now, which makes view controllers a little bit thinner.
创建VIP模式是为了解决VIPER体系结构的某些问题。 一个问题是Presenter
( P
)组件可能会变得太大-因为它负责处理UI动作并为视图准备数据。 为了促进单一责任原则,更改了对象之间的关系。 VIPER的V
部分同时表示ViewController
和UIView
,而VIP的“ V
”仅表示ViewController
。 因此, UIView
现在是一个单独的组件,这使视图控制器更薄一些。
Before we start, let’s understand the terminology behind the VIP pattern. Each module or scene usually involves the following components with their corresponding responsibilities:
在开始之前,让我们了解VIP模式背后的术语。 每个模块或场景通常包含以下组件,并具有相应的职责:
View
: Anything that is aUIView
subclass. It should be reusable and as passive as possible.View
: 任何属于UIView
子类的东西。 它应该是可重用的并且尽可能被动。ViewController
: An object that is created to manage the behavior of a specific view. May act as a data source or event handler. Calls necessaryInteractor
’s methods in response to view events and receives raw view-representable data from thePresenter
.ViewController
: 创建用于管理特定视图行为的对象。 可以充当数据源或事件处理程序。 调用必要的Interactor
方法以响应视图事件,并从Presenter
接收可表示视图的原始数据。Interactor
: Performs business logic in the app, but is not aware of lower-level implementations, such as network clients or databases. For this, we have dependencies on services that do know about them and use them directly. When a service’s task is performed, theInteractor
obtains the result and sends it to thePresenter
.Interactor
:在应用程序中执行业务逻辑,但不了解较低级别的实现,例如网络客户端或数据库。 为此,我们依赖确实知道它们并直接使用它们的服务。 执行服务的任务时,Interactor
获取结果并将其发送给Presenter
。Presenter
: Receives a task’s result from the interactor and transforms it into a view-suitable format. For example, it may transform an array of complicated Core Data objects into a simple array ofString
sPresenter
:从交互器接收任务的结果,并将其转换为适合视图的格式。 例如,它可以将复杂的Core Data对象数组转换为String
的简单数组。Router
: Responsible for performing navigation inside the app. Typically has a dependency on aUINavigationController
that’s provided by aConfigurator
.Router
:负责在应用内执行导航。 通常依赖于Configurator
提供的UINavigationController
。Configurator
: An object that creates a module by linking all the previous components.Configurator
:通过链接所有先前的组件来创建模块的对象。Entity
: A plain object, such as a Core Data entity or aCodable
model.Entity
:普通对象,例如核心数据实体或可Codable
模型。
Take a look at this diagram (an object that’s being pointed to is owned by the pointing object):
看一下此图(指向的对象归指向对象所有):