iOS 组件化方案

时间:2022-12-29 21:59:43

概述

近一年iOS业界讨论组件化方案甚多,大体来说有3种。

  • Protocol注册方案

  • URL注册方案

  • Target-Action runtime调用方案

URL注册方案据我了解很多大公司都在采用,蘑菇街 App 的组件化之路(http://limboy.me/tech/2016/03/10/mgj-components.html)蘑菇街的Limboy在这篇博客中做了很详尽的阐述

Target-Action runtime调用方案Casa在 iOS应用架构谈 组件化方案(http://casatwy.com/iOS-Modulization.html)中也做了很详尽的描述,前阵时间Casa开了一篇博客在现有工程中实施基于CTMediator的组件化方案清楚讲述了如何用这套方案实施组件化

Protocol方案我尚未看到有人做过详尽的分享,也许是我孤陋寡闻,不过在这里,我会教大家用Protocol方案实施(http://casatwy.com/modulization_in_action.html)组件化,不仅如此..

我会采用以上3种方案详尽的实现3个Demo,Demo会在文尾给到,本文不过多阐述3种方案的优劣,我会在最后做一个总结,希望给想了解组件化方案的同学或者给在项目中准备实施组件化方案的同学提供一个借鉴。

业务模拟场景

  • 首页展示商品列表

  • 商品详情页展示商品的详细信息

  • 确认订单页生成订单

把业务连贯起来 点击首页中A商品,进入A商品的商品详情页 ,点击商品详情页中的 立即购买 进入确认订单页,点击确认订单页中的提交订单 会返回到商品详情页,并且在商品详情页中告知用户下单成功.

真实业务场景下确认订单页 点提交订单 是不会回到商品详情页的,模拟这个场景是想在Demo中实现2个模块中反向回调。

一、Protocol注册方案

正式实施前先奉上Demo,建议只下一个主项目就可以了(注:下载完不需要pod install或者pod update,pods在我私有源上 我没有填写.gitignore文件,下载完都是可以直接跑的)

主项目地址

https://github.com/sun6boys/CRMainProject

商品详情业务接口组件地址

https://github.com/sun6boys/CRGoodsDetailServiceProtocol

商品详情业务组件地址

https://github.com/sun6boys/CRGoodsDetail

确认订单业务接口组件地址

https://github.com/sun6boys/CRConfirmOrderServiceProtocol

确认订单业务组件地址

https://github.com/sun6boys/CRConfirmOrder

业务调度中间件地址

https://github.com/sun6boys/CRProtocolManager

1.基本准备工作

  • 先去gitHub创建一个项目存放私有Repo源,repo地址https://github.com/sun6boys/CRRepositories.git 后面3种方案私有pod源都会放在这里。

  • 本地添加私有源 终端执行命令pod repo add CRRepositories https://github.com/sun6boys/CRRepositories.git(如果之前并未向gitHub push过文件也没有把SSH公钥保存到gitHub,这时候应该会提示你输入gitHub账号密码)

  • 以上操作完成 cd ~/.cocoapods/repos目录下至少会有2个文件夹 *CRRepositories 和 master, master文件下面存放的是公有源文件,CRRepositories目录下目前是空的,后面会存放我们私有源文件

  • 基本准备工作完成。

2.Xcode创建项目[CRProtocolManager]

CRProtocolManager和MGJRouter、CTMediator一样属于模块之间调度的中间件

在CRProtocolManager项目下创建名为CRProtocolManager的文件夹,后面我们需要做成私有pod的文件均放在该文件夹下。

创建CRProtocolManager类(.h,.m),定义2个对外接口

@interface CRProtocolManager : NSObject

+ (void)registServiceProvide:(id)provide forProtocol:(Protocol*)protocol;

+ (id)serviceProvideForProtocol:(Protocol *)protocol;

@end

具体方法实现很简单可以参看Demo,我这里只是简单处理。

接下来就是要把项目提交到gitHub,做私有pod了

  • gitHub新建一个project名为CRProtocolManager

  • 终端cd至CRProtocolManager项目目录下执行命令git remote add origin https://github.com/sun6boys/CRProtocolManager.git

  • 因cocoaPods强制添加开源许可文件执行命令echo MIT>FILE_LICENSE创建名为FILE_LICENSE的文件

  • 终端cd至CRProtocolManager目录下执行命令pod spec create CRProtocolManager

  • 执行命令vim .CRProtocolManager.podspec编辑podspec文件,具体如何编辑可参看Demo中的podspec文件或者google

  • 退出编辑执行命令git add .

  • `git commit -m ‘log’

  • git tag 0.0.1 tag一定要和podspec中的version一致

  • git push origin master --tags –tags为了把刚才添加的tag提交上去

  • 执行命令pod repo push CRRepositories CRProtocolManager.podspec --verbose --allow-warnings 注:CRRepositories即为准备工作中的私有源仓库

  • 成功后pod search CRProtocolManager应该就能搜索到了

万里长征终于走完第一步,基础设施已经构建完毕

3.商品详情业务模块

既然组件化了,那我们所有的业务模块都是单独的project,但是这里我会分2个project,一个是商品详情业务入口模块,一个是商品详情业务模块。业务入口模块即是定义该模块对外提供业务接口的protocol,如果A模块需要调用到B模块,那A模块只需要引入CRProtocolManager和B模块的protocol,而不是引入整个B模块。

新建一个projectCRGoodsDetailServiceProtocol,创建一个和项目名一样的protocol文件,定义接口如下

@protocol CRGoodsDetailServiceProtocol

@required;

- (UIViewController *)goodsDetailViewControllerWithGoodsId:(NSString*)goodsId goodsName:(NSString *)goodsName;

@end

参照CRProtocolManager做成私有pod

以上实施完毕,新建一个projectCRGoodsDetail,新建2个类

CRGoodsDetailServiceProvide
CRGoodsDetailViewController

CRGoodsDetailServiceProvide即是CRGoodsDetailServiceProtocol的实现者 所以他依赖

CRGoodsDetailServiceProtocol,因为商品详情模块需要跳转到订单确认页,所以他也依赖CRProtocolManager。

添加Podfile文件编辑如下

source 'https://github.com/sun6boys/CRRepositories.git'
source 'https://github.com/CocoaPods/Specs.git' target 'CRGoodsDetail' do pod "CRProtocolManager"
pod "CRGoodsDetailServiceProtocol"

执行pod install --verbose --no-repo-update

最终CRGoodsDetailServiceProvide实现代码如下

#import "CRGoodsDetailServiceProvide.h"
#import
#import #import "CRGoodsDetailViewController.h" @interface CRGoodsDetailServiceProvide() @end @implementation CRGoodsDetailServiceProvide + (void)load
{
[CRProtocolManager registServiceProvide:[[self alloc] init] forProtocol:@protocol(CRGoodsDetailServiceProtocol)];
} - (UIViewController *)goodsDetailViewControllerWithGoodsId:(NSString*)goodsId goodsName:(NSString *)goodsName
{
CRGoodsDetailViewController *goodsDetailVC = [[CRGoodsDetailViewController alloc] initWithGoodsId:goodsId goodsName:goodsName];
return goodsDetailVC;
} @end

CRGoodsDetailViewController实现代码如下

#import "CRGoodsDetailViewController.h"

@interface CRGoodsDetailViewController ()

@property (nonatomic, copy) NSString *goodsId;
@property (nonatomic, copy) NSString *goodsName; @property (nonatomic, strong) UILabel *statusLabel;
@property (nonatomic, strong) UIButton *buyButton;
@end @implementation CRGoodsDetailViewController - (instancetype)initWithGoodsId:(NSString *)goodsId goodsName:(NSString *)goodsName
{
self = [super init];
if (self) {
_goodsId = goodsId;
_goodsName = goodsName;
}
return self;
} - (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = self.title; [self.view addSubview:self.statusLabel];
[self.view addSubview:self.buyButton];
} - (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.statusLabel.frame = CGRectMake(, , , );
self.statusLabel.center = self.view.center; self.buyButton.frame = CGRectMake(, self.view.frame.size.height - , self.view.frame.size.width, );
} #pragma mark - event
- (void)didClickBuyButton:(UIButton *)button
{ } #pragma mark - getters
- (UILabel *)statusLabel
{
if (_statusLabel == nil) {
_statusLabel = [[UILabel alloc] init];
_statusLabel.textColor = [UIColor redColor];
_statusLabel.font = [UIFont systemFontOfSize:.f];
_statusLabel.textAlignment = NSTextAlignmentCenter;
_statusLabel.text = @"暂未购买";
}
return _statusLabel;
} - (UIButton *)buyButton
{
if (_buyButton == nil) {
_buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_buyButton setTitle:@"立即购买" forState:UIControlStateNormal];
[_buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_buyButton setBackgroundColor:[UIColor redColor]];
[_buyButton addTarget:self action:@selector(didClickBuyButton:) forControlEvents:UIControlEventTouchUpInside];
}
return _buyButton;
}
@end

把CRGoodsDetail做成私有pod 记得编辑podspec文件的时候添加dependencyCRProtocolManager CRGoodsDetailServiceProtocol

4.新建主项目MainProject

为了少建一个项目首页模块我是直接放在主项目中的,按理首页也应该是一个独立的pod.

首页业务场景是,显示商品列表,点击某个商品进入该商品详情页. 所以他依赖CRGoodsDetailServiceProtocol和CRProtocolManager因为首页模块即是主项目所以他还得依赖CRGoodsDetail

最终首页核心代码如下

#pragma mark - event
- (void)didClickGoodsButton:(UIButton *)button
{
id goodsServiceProvide = [CRProtocolManager serviceProvideForProtocol:@protocol(CRGoodsDetailServiceProtocol)];
UIViewController *goodsDetailVC = [goodsServiceProvide goodsDetailViewControllerWithGoodsId:@"" goodsName:@"农夫山泉矿泉水"];
[self.navigationController pushViewController:goodsDetailVC animated:YES]; }

5.确认订单模块

参照商品详情新建确认订单业务入口pod 以及确认订单业务pod.和商品详情有区别的是,提交订单完成后要回到商品详情并且通知商品详情用户已经购买,所以CRConfirmOrderServiceProtocol接口定义如下

@protocol CRConfirmOrderServiceProtocol

- (UIViewController *)confirmOrderViewControllerWithGoodsId:(NSString *)goodsId sureComplete:(dispatch_block_t)sureComplete;

@end

最后记得在商品详情加上跳转并且podspec里面加上dependency

Protocol注册方案完结

iOS 组件化方案的更多相关文章

  1. iOS组件化方案

    一.蘑菇街url-block方案 这是蘑菇街中应用的一种页面间调用的方式,通过在启动时注册组件提供的服务,把调用组件使用的url和组件提供的服务block对应起来,保存到内存中.在使用组件的服务时,通 ...

  2. iOS应用架构谈 组件化方案

    转载: iOS应用架构谈 组件化方案 简述 前几天的一个晚上在infoQ的微信群里,来自蘑菇街的Limboy做了一个分享,讲了蘑菇街的组件化之路.我不认为这条组件化之路蘑菇街走对了.分享后我私聊了Li ...

  3. iOS组件化实现方案

    作者原文iOS组件化 - 路由架构从0到1实战  合伙呀 1.CTMediator作为路由中间件 2.基础UI组件以pod形式引入,并且能够独立运行调试 3.基础工具组件以pod形式引入,并且能够独立 ...

  4. iOS组件化思路 <转>

    随着应用需求逐步迭代,应用的代码体积将会越来越大,为了更好的管理应用工程,我们开始借助CocoaPods版本管理工具对原有应用工程进行拆分.但是仅仅完成代码拆分还不足以解决业务之间的代码耦合,为了更好 ...

  5. iOS组件化思路-大神博客研读和思考

    一.大神博客研读 随着应用需求逐步迭代,应用的代码体积将会越来越大,为了更好的管理应用工程,我们开始借助CocoaPods版本管理工具对原有应用工程进行拆分.但是仅仅完成代码拆分还不足以解决业务之间的 ...

  6. iOS 组件化 —— 路由设计思路分析

    原文 前言 随着用户的需求越来越多,对App的用户体验也变的要求越来越高.为了更好的应对各种需求,开发人员从软件工程的角度,将App架构由原来简单的MVC变成MVVM,VIPER等复杂架构.更换适合业 ...

  7. iOS 组件化的几篇文章

    随着工程的成长,开发人员的增多,合理的模块划分及低耦合的重要性显得愈发重要.最近在思考这方面的问题,也读了不少通过组件化解耦的文章,这里记录一下. 前 5 篇文章有些关联,建议阅读顺序,1.3.2.4 ...

  8. iOS 组件化路由框架 WisdomRouterKit 的应用

    [前言] 大家好,写作是为了和读者沟通交流,欢迎各位开发者一起了解 WisdomRouterKit SDK 的功能. 关于 iOS 组件化路由方案框架: WisdomRouterKit 的功能介绍,之 ...

  9. Vue.js:轻量高效的前端组件化方案

    转发一篇尤老师对vue.js的介绍,了解vue.js的来龙去脉.不过现在已经是2.0了,也有添加一些新的东西,当然有些东西也改了. Vue.js:轻量高效的前端组件化方案 Vue.js 是我在2014 ...

随机推荐

  1. AngularJs之四(作用域)

    一:angulaJs的作用域scope Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带.scope 是一个 JavaScript 对象,带有属性和方 ...

  2. C# 配置文件 AppSettings和ConnectionStrings的区别

      web.config是web应用程序的配置文件,为web应用程序提供相应配置(B/S).app.config是桌面应用程序的配置文件,为桌面应用程序提供相应配置(C/S).Configuratio ...

  3. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  4. 下拉框点链接js

    $("#input_text").click(function(){ $("#input_fonts").show(); }); $("#input_ ...

  5. PHP中取出字符串中的空格 逗号

    preg_replace("/\s| |,|,/","",$_str) PHP中取出字符串中的空格 逗号 (包括中文状态下)

  6. hadoop 2.x 简单实现wordCount

    简单实现hadoop程序,包括:hadoop2.x的实现写法 import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs ...

  7. synchronized与volatile的区别及各自的作用、原理(学习记录)

    synchronized与volatile的区别,它们的作用及原理? 说到两者的区别,先要了解锁提供的两种特性:互斥(mutual exclusion) 和可见性(visibility). 互斥:即一 ...

  8. ubuntu 关闭 笔记本键盘背景灯

    /etc/rc.local 加入 ' > /sys/class/leds/tpacpi::kbd_backlight/brightness

  9. lucene笔记

    lucene全文检索 全文检索是计算机程序通过扫描文章中的每一个词, 对每一个词建立一个索引, 指明该词在文章中出现的次数和位置. 当用户查询时根据建立的索引查找,类似于通过字典的检索字表查字的过程

  10. python源码安装

    # mkdir /apps/Python- 解压源码包,进入源码包 [root@LB_81 Python-]# ls aclocal.m4 configure.ac install-sh Makefi ...