在iOS应用开发中,我们一般会将应用分为3层: View - Controller - Model, 我们这里所说的中间层其实是指Model层。对于Model层我们还可以细分为2层: Manager - Data,即原始数据+数据接口;对于Manager层,在我们原先的做法,是对于各种Data我们建议独立的Manager,如NetManager、ThumbManager、FileManager。随着功能的不断添加,会创建越来越多的Manager,而这些Manager都是独立,无从管理,从而就会显得整个工程比较混乱。这里我提出一种来管理新的解决思路 - 服务模型. 各种Model通过服务来管理,只有对此服务进行注册,启动后才能提供相应的服务.

前提

  • 获取所有子类
\+ (NSArray *) allSubclasses
{
    Class myClass = [self class];
    NSMutableArray *mySubclasses = [NSMutableArray array];
    
    unsigned int numOfClasses;
    Class *classes = objc_copyClassList(&numOfClasses);
    for (unsigned int ci = 0; ci < numOfClasses; ci++) {
        Class superClass = classes[ci];
        do {
            superClass = class_getSuperclass(superClass);
        } while (superClass && superClass != myClass);
        if (superClass)
            [mySubclasses addObject: classes[ci]];
    }
    free(classes);
    
    return mySubclasses;
}

服务中心(ServiceCenter)

ServiceCenter.h

\#import <Foundation/Foundation.h>
\#define __serviceCenter SINGLETON_CALL(ServiceCenter)

@interface ServiceCenter : NSObject
SINGLETON_AS(ServiceCenter)

/**
 * 启动服务,用于启动所有的Serivce服务
 *
 */
\- (void)startAllServices;

/**
 * 关闭服务,用于关闭所有的Serivce服务
 *
 */
\- (void)stopAllServices;

\- (void)setup;

\- (void)teardown;

ServiceCenter.c

@interface ServiceCenter() {
    NSMutableArray* _serviceArray;
}
@end

@implementation ServiceCenter

SINGLETON_DEF(ServiceCenter)

\- (instancetype)init
{
    self = [super init];
    if (self) {
        _serviceArray = [NSMutableArray array];
    }
    return self;
}

\- (void)setup {
    NSArray* classes = [BaseService allSubclasses];
    for (Class cls in classes) {
        BaseService* service = [[cls alloc] initWithServiceCenter:self];
        [_serviceArray addObject:service];
    }
}



\- (void)teardown {
    for (BaseService* service in _serviceArray) {
        [service stop];
    }
    [_serviceArray removeAllObjects];
}

\- (void)startAllServices
{
    for (BaseService* service in _serviceArray) {
        [service start];
    }
}

\- (void)stopAllServices
{
    for (BaseService* service in _serviceArray) {
        [service stop];
    }
}

@end

服务基类

BaseService.h

@interface BaseService : NSObject

@property(nonatomic, strong) ServiceCenter*  serviceCenter;
\- (id)initWithServiceCenter:(ServiceCenter*)serviceCenter;

\- (void)start;

\- (void)stop;

@end

BaseService.c

@implementation BaseService

\- (id)initWithServiceCenter:(ServiceCenter*)serviceCenter
{
    self = [super init];
    if (self) {
        self.serviceCenter = serviceCenter;
    }
    return self;
}

\- (void)start {
    //启动服务,子类实现
}

\- (void)stop {
    //停止服务,子类实现
}

@end

各种服务子类

  • 设计思路:
    • 通过类Category特性,针对每个类,定义ServiceCenter的相应扩展
    • 在ServiceCenter扩展中定义相应的方法接口及属性,其中属性可以通过类关联构建(objc_getAssociatedObject和objc_setAssociatedObject)
    • 当ServiceCenter扩展接口需要访问该服务子类,则可以定义该服务的属性
  • 样例

    DataService.h

    @class DataService;
    @interface ServiceCenter (DataService)
    @property (nonatomic, strong) DataService* dataService;
    //其他接口和属性声明
    @end
    @interface DataService : BaseService
    //接口和属性
    @end
    

</pre>

DataService.c
<pre> @implementation ServiceCenter (DataService) CATEGORY_PROPERTY_GET_SET(DataService*, dataService, setDataService:) //其他接口和属性实现 @end @implementation DataService \- (id)initWithServiceCenter:(ServiceCenter*)serviceCenter {
self = [super initWithServiceCenter:serviceCenter];
if (self) {
    serviceCenter.dataService = self;
	...
}
return self; } @end

</pre>