Boardy
为什么选择 Boardy?
《微系统》或《微服务》是一种特别有效的架构,用于开发在需求方面存在较大、复杂和不断变化系统的结构。
在移动应用开发环境中,由于技术障碍,难以正确实现微系统。虽然微系统重视独立性,但移动应用的组件通常运行在紧密耦合中。或者,由于微系统以动态和灵活的方式相互交互,移动应用的组件则更欣赏绑定和类型安全的资料。然而,可以将微系统设计理论和原则以定制方式应用于移动应用,使其能够像微系统一样运行。适用于具有高复杂性和扩展要求的程序。
构建微系统架构的两个原则
-
通过消除和优化组件的 依赖关系,使其 隔离。这使得将组件作为包打包变得容易,并且可以在系统的任何地方 部署。
-
使用一组独特的 协议 与系统中的所有组件进行通信。因此,一个组件可以与任何其他组件 交互。从外部来看,它们完全相同,并且可以在不更改核心业务的情况下进行 互换。
Boardy 帮助以简单的方式构建移动应用微系统。它是在计算机主板的启发下建立的。Boardy 基于板构建微系统。每个板对应于其中包含的微系统,这是移动应用的一个组件,将执行特定的任务。所有板随后整合到一个主板中,形成完整的系统。组件之间不直接交互,而是通过标准的 Boardy 协议套件交互。核心业务逻辑受到保护,免受更改和外部影响。只需更改板的整合方式就能够创建新的业务,因此更改或扩展变得容易,确保同时满足维护和开发要求。
示例
要运行示例项目,请先克隆仓库,然后在 Example 目录中运行 pod install
。
需求
- iOS 10+
- Xcode 11+
- Swift 5.1+
安装
Boardy 可以通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile
pod 'Boardy'
安装 subspecs 以获得更多功能
# Utilities toolkit
pod 'Boardy/ComponentKit'
# Modularization
pod 'Boardy/ModulePlugin'
# Build a complex UI using Boardy
pod 'Boardy/Composable'
安装模板以开发功能
- 克隆 module template 仓库
- 运行
./install-template.sh
- 重启 Xcode
添加新功能
在 Xcode 中右键点击,选择 新建文件...,然后选择 Boardy
模板,输入 名称
并点击 下一步,选择文件位置并 创建。
将创建一个新的功能组件,包含一个 板、一个 IO接口、一个带有 Builder 模式的 视图控制器或无视图控制器。
Boardy 1.19+ 引入了 IOInterface
来在微服务之间进行通信(您可以使用上述模板生成自定义的公共 IOInterface
)。这有助于确保微服务的 输入
和 输出
值一致,实现 类型安全的交互。
注意:您需要在
YourInOut.swift
中检查和更新您为组件选择的正确输入和输出类型(默认情况下,输入输出为 Optional Any)。
从 Boardy 1.27+ 开始,包括 ModulePlugin
。因此,只需添加以下 subspec
pod "Boardy/ModulePlugin"
您可能需要在 Integration/YourModulePlugin.swift
中为 Your Board
正确位置添加 BoardRegistration
。位置取决于您的流程结构。一个 Motherboard
管理一个业务流程,一个 continuousBoard
管理一个子流程。
☞ 否则,您需要向 BoardProducer 添加注册以提供您的 Board 构造函数
BoardProducer
是一个工厂,它帮助 Motherboard
在首次激活时懒加载子 Board。这在 Motherboard
不需要立即初始化所有 Board 可能导致性能问题时非常有用。
BoardRegistration(.yourFeature) { identifier in
YourBoard(identifier: identifier, builder: YourBuilder())
}
Board
与其他功能组件通信:
您使用 - 要激活
OtherFeature
作为子流程,请在IOInterface
中使用activation
func openOtherFeature() {
motherboard.ioOtherFeature().activation.activate()
}
- 要处理来自
Other Feature
的回调,注册一个流程,在IOInterface
中使用flow
处理器
func registerFlows() {
motherboard.ioOtherFeature().flow.addTarget(self) { target, output in
target.handleOutput(output)
}
}
- 要向
Motherboard
发送输出数据,请使用sendOutput
方法
func yourFeatureDidComplete() {
self.sendOutput("Output data")
}
- 要与 内部控制器 交互,请使用 事件总线
...
// Declare bus with data type String for example
private let eventBus = Bus<String>()
...
// Bind the bus to Controller to get data
func activate(withGuaranteedInput input: InputType) {
let component = builder.build(withDelegate: self)
let viewController = component.userInterface
motherboard.putIntoContext(viewController)
rootViewController.show(viewController)
eventBus.connect(target: component.controller) { controller, data in
controller.updateSomething(data)
}
}
// Transport data to bus, for example from OtherFeature callback
func registerFlows() {
motherboard.ioOtherFeature().flow.bind(to: eventBus)
}
// Or send a custom event
func sendCustomEvent(value: String) {
eventBus.transport(value)
}
引用
- 移动应用的小型系统
- Boardy 模块化
- 处理URL打开/深度链接
- 为您的Board配置激活屏障
- Boardy ComponentKit
- HelloBoardy - Boardy示例:第一部分+第二部分 - 基本功能,第三部分 - Boardy模块化
作者
congncif, [email protected]
许可证
Boardy许可为MIT授权。相关信息见LICENSE文件。