プログラムアーキテクチャ設計案の1-解結合
1656 ワード
プログラムコード解結合の利点は多い:1:プログラムの多重化可能2:テスト可能3:清潔4:ファイル関連を減らす
次に、私のコードを例に挙げます(コードは一般的に、スプレーしてください)'''
import "NetManger.h"
次に、私のコードを例に挙げます(コードは一般的に、スプレーしてください)'''
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
'''
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
'''
@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
'''