程序架构设计方案之1--解耦合

程序代码解耦合好处多多:
1:程序的可复用
2:可测试
3:整洁
4:减少文件关联

下面以我的代码为实例(代码一般,请喷):
'''

import "NetManger.h"

import "BaseModel.h"

import

import "AFNetRequest.h"

@implementation NetManger
+(instancetype)shareInstance{
static NetManger* manger;
if (!manger) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manger = [[self alloc] init];
});
}
return manger;
}
-(id)getDataFromServer:(NSDictionary)dic className:(NSString)className urlKey:(NSString)urlKey complete:(void (^)(id))complete failed:(void (^)(NSError))failed error:(void(^)(id ))error{

id  newClass=   [[NSClassFromString(className) alloc] init];

BaseModel *base = [BaseModel new];
base = (BaseModel*)newClass;

[AFNetRequest postData:urlKey data:dic sucess:^(id responseObject) {
    
    ResponseData* data = [self parseAll:responseObject];
    if ([responseObject[@"code"] integerValue] == 0) {
        complete([base initWithDictionary:data.data]);
    }else{
        error(responseObject);
    }
    
} failure:^(NSError *error) {
    failed(error);
}];


return nil;

}

-(ResponseData)parseAll:(id)responseObject{
NSDictionary
jsonResult = responseObject;
ResponseData* data = [[ResponseData alloc] initWithDictionary:jsonResult];

NSDictionary* dic = [jsonResult dictionaryValueForKey:@"result"];
data.data = dic;
if (dic.count == 0) {
    NSArray *array = [jsonResult arrayValueForKey:@"result"];
    data.data = array;
}

return data;

}

@end

'''

你可能感兴趣的:(程序架构设计方案之1--解耦合)