AWMaster 1.1.1

AWMaster 1.1.1

Aiwei 维护。



AWMaster 1.1.1

  • aiweichujian

AWMaster

CI Status Version License Platform

描述

AWMaster是组件化架构中的中间件,核心是消息转发和面向接口编程。组件可以直接通过Master调用方法来获取服务,调用时支持任意公共类型参数的传递,同时也提供通过urlPath进行服务调用,提供相关接口来对异常调用做集中处理,还提供公告功能来将消息转发给所有想要接收的组件。

安装

AWMaster支持通过 CocoaPods 进行安装:

pod 'AWMaster'

用法

组件服务

AWMaster使用Master-Slave的主从结构,使用时创建一个继承自AWMaster的子类作为中间件,在提供服务的组件中创建一个Slave类,然后制定一份服务协议(Protocol),让中间件Master和Slave都遵循这份协议:

// MoudleAProtocol.h
@protocol MoudleAProtocol <NSObject>
+ (UIImage *)moudleA_imageWithImageName:(NSString *)imageName;
@end

// MyMaster_A.h
@interface MyMaster : AWMaster
<
    MoudleAProtocol
>
@end

// MySlave_A.h
@interface MySlave_A : NSObject<MoudleAProtocol>

@end

然后在Slave的load方法中注册Slave为协议的执行者:

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [MyMaster registerSlave:self withProtocol:@protocol(MoudleAProtocol)];
    });
}

最后,在Slave中实现服务协议的内容,当其他组件需要调用服务时,通过Master即可调用服务协议中的方法,Master内部会将方法转发给提供服务的Slave:

UIImage *image = [MyMaster moudleA_imageWithImageName:@"preview_1.png"];

远程调用

在AWMaster中,可以通过urlPath调用服务以满足远程调用的需求,urlPath格式为scheme://[methodType]/[selector]?[params],例如以上的调用也可以这样进行:

UIImage *image = [MyMaster openServiceWithUrlPath:@"image://class/moudleA_imageWithImageName:?preview_1.png"];

公告通知

在组件化架构中,有时可能需要将一条消息通知给其他多个组件,AWMaster提供了公告(Announcement)功能。公告是一个协议,与服务协议不同的是,它需要在中间件中向receiverArray中的Slave广播。AWMater实现了对APP生命周期相关的一些方法的分发,在中中可以选择遍历receiverArray来分发公告:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions
{
    NSMutableDictionary *results = [[NSMutableDictionary alloc]init];
    for (Class cls in [self.class receiverArray]) {
        if ([cls instancesRespondToSelector:_cmd]) {
            BOOL finished = [[cls new] application:application didFinishLaunchingWithOptions:launchOptions];
            results[NSStringFromClass(cls)] = @(finished);
        }
    }
    return YES;
}

也可以使用AWMaster+Tools中封装的宏AW_AnnounceCurrentMethod(...)来将消息转发给所有需要接收的Slave:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    AW_AnnounceCurrentMethod(application);
}

Demo

repo中包含一个demo,运行demo之前,需要先执行pod install

许可证

AWMaster支持MIT许可证协议,详情见LICENSE文件。