美文网首页
『ios』组件化-组件之间的业务解耦

『ios』组件化-组件之间的业务解耦

作者: butterflyer | 来源:发表于2021-03-03 19:51 被阅读0次

上一篇讲了一下目前页面跳转路由之间的解耦。
这一篇就来说一些组件之间业务解耦。

你觉得应该怎么来实现组件之间的业务解耦?听我来讲吧。

有没有想过通过协议来进行组件化之间的解耦。
针对不同的组件创建不同的ServiceProtocol.
就比如登录模块,我们可以这么写。

@protocol LoginServiceProtocol <RouterProviderProtocol>
/// 用户id
- (NSString *)getUserId;
/// 退出登录提示,后端返回文本
- (void)loginOutWithDialog:(NSString *)text;

/// renlei  MyLogout
- (void)loginOutWithSuccess:(void(^)(id lParam,id rParam))success
                       Fail:(void(^)(id lParam,id rParam))fail;
@end

我们需要在app启动的时候,拿取所有的 继承RouterProviderProtocol的协议。
我这边是写到路由的RouterConfiguration单例中的


- (void)initializationData {
        
    NSMutableArray *appDelegateClass = [NSMutableArray array];

    NSMutableSet *modularsSet = [NSMutableSet set];
    
    NSMutableDictionary *provideServiceDict = [NSMutableDictionary dictionary];
    
    unsigned int outCount = 0;
    
    Class *classes = objc_copyClassList(&outCount);
    
    for (int i = 0; i < outCount; i++) {
        
        Class cla = classes[i];
                
        NSString *claName = NSStringFromClass(cla);
        
    
        /// 供应者服务
        if (class_conformsToProtocol(cla, @protocol(ZPMRouterProviderProtocol))) {
            
            unsigned int count;
            
            Protocol * __unsafe_unretained * protocolList = class_copyProtocolList(cla, &count);
            
            for (NSInteger i = 0; i < count; i++) {
                
                Protocol *protocol = protocolList[i];
                
                if (protocol_conformsToProtocol(protocol, @protocol(ZPMRouterProviderProtocol))) {
                    
                    NSString *key = [NSString stringWithUTF8String:protocol_getName(protocol)];
                    [provideServiceDict setObject:cla forKey:key];
                    break;
                }
            }
        }
    }
    free(classes);
    
    self.provideServiceDict = [provideServiceDict copy];
}

现在我们所有的协议对象都存在了provideServiceDict中。

我们新建一个appService服务对象。
就比如新建一个loginSerivice变量,通过重写他的get方法。
去拿取对应的service类对象。

+ (id<loginServiceProtocol>)loginService {
    return [[Router router] getProviderService:@protocol(loginServiceProtocol)];
}
- (id)getProviderService:(Protocol *)protocol {
    
    Class<ZPMRouterProviderProtocol> aCla = [self getProviderServiceClass:protocol];
    BOOL isResponds = [aCla respondsToSelector:@selector(routerProviderInstance)];
    NSAssert(isResponds, @"%@ 未实现 【routerProviderInstance】",NSStringFromProtocol(protocol));
    if (isResponds) {
        return [aCla routerProviderInstance];
    }
    return nil;
}

- (Class)getProviderServiceClass:(Protocol *)protocol {
    
    NSString *key = NSStringFromProtocol(protocol);
    if (key) {
        return [ZPMRouterConfiguration getProvideServiceDict][key];
    }
    return nil;
}

在loginService中实现routerProviderInstance方法。来新建这个服务的实例。

+ (id)routerProviderInstance {
    return [Service sharedInstance];
}

+ (instancetype)sharedInstance {
    static loginService *login = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        login = [[loginService alloc] init];
    });
    return login;
}

loginservice对象需要实现loginServiceProtocol协议,并实现相应的方法。
下面我们就可以在其他组件中,通过这个实例对象来处理这个组件中的业务逻辑,从而实现组件之间的解耦

  [[APPService loginService] getUserId];

相关文章

  • 『ios』组件化-组件之间的业务解耦

    上一篇讲了一下目前页面跳转路由之间的解耦。这一篇就来说一些组件之间业务解耦。 你觉得应该怎么来实现组件之间的业务解...

  • iOS组件化方案实战

    目录 简述 为什么要项目组件化 组件化架构思路 业务模块解耦 组件化实施流程解耦主题国际化切换PrefixHead...

  • iOS组件化/模块化 APP方案实践篇

    1.博客文章: [模块化与解耦](模块化与解耦 - 刘坤的技术博客) 浅析 iOS 应用组件化设计 [iOS组件化...

  • 面试3

    12、iOS组件化 iOS组件化及架构设计关于组件化网上组件化的文章很多。很多文章一提到组件化,就会说解耦,一说到...

  • iOS组件化储备

    资料 组件化/模块化 蜂鸟商家版 iOS 组件化 / 模块化实践总结 模块化与解耦 浅析 iOS 应用组件化设计 ...

  • iOS 组件化(一)

    组件化 组件化就是将模块单独抽离,分层,通过制定的通讯方式,实现解耦 组件化优点 模块间的解耦 模块重用 提交团队...

  • ios组件化/模块化

    1.博客文章: [模块化与解耦](模块化与解耦 - 刘坤的技术博客) [浅析 iOS 应用组件化设计](Skyli...

  • iOS组件化及架构设计(转)

    一篇开源代码的组件化方案 关于组件化 网上组件化的文章很多。很多文章一提到组件化,就会说解耦,一说到解耦就会说路由...

  • 给广大 Android 开发者的一份组件化架构指南:从理论到实战

    为什么需要组件化? 极大提高工程编译速度 业务模块解耦,有利于多人团队协作开发 什么是组件化? 所谓的组件化就是把...

  • 打造完备的 iOS 组件化方案:如何面向接口进行模块解耦?(一)

    打造完备的 iOS 组件化方案:如何面向接口进行模块解耦? 关于组件化的探讨已经有不少了,在之前的文章iOS VI...

网友评论

      本文标题:『ios』组件化-组件之间的业务解耦

      本文链接:https://www.haomeiwen.com/subject/mircqltx.html