解决“单接口,多类”架构设计的回调问题

全力推动信息,而非照顾他人感受

Github 源码:https://github.com/libinggen/CallBack
最终效果:

解决“单接口,多类”架构设计的回调问题_第1张图片
最热主题
解决“单接口,多类”架构设计的回调问题_第2张图片
最新主题

工程目录:

解决“单接口,多类”架构设计的回调问题_第3张图片
工程目录

代码:

APIInterface.h

#ifndef CallBack_APIInterface_h
#define CallBack_APIInterface_h
@protocol APIInterface 

- (void)loadData;
@end
#endif

APIParamSourceInterface.h

#import "APIInterface.h"

#ifndef CallBack_APIParamSourceInterface_h
#define CallBack_APIParamSourceInterface_h
@protocol APIParamSourceInterface 

- (NSDictionary *)paramSourceAPIHandle:(id)apiHandle;
@end
#endif

APICallbackInterface.h

#import "APIInterface.h"

#ifndef CallBack_APICallbackInterface_h
#define CallBack_APICallbackInterface_h
@protocol APICallbackInterface 

- (void)listWtihData:(NSArray *)data apiHandle:(id)apiHandle;
@end
#endif

APIHandle.h

#import 
#import "APIInterface.h"
#import "APIParamSourceInterface.h"
#import "APICallbackInterface.h"

@interface APIHandle : NSObject
@property (nonatomic, strong) NSMutableData *receiveData;
@property (nonatomic, weak) id paramSourceHandle;
@property (nonatomic, weak) id callbackHandle;
@end

APIHandle.m

#import "APIHandle.h"

@implementation APIHandle

- (void)loadData
{
    self.receiveData = [[NSMutableData alloc]init];
    NSDictionary *paramSource = [self.paramSourceHandle paramSourceAPIHandle:self];
    NSURL *url = [[NSURL alloc]initWithString:paramSource[@"URLString"]];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receiveData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *error = nil;
    id data = [NSJSONSerialization JSONObjectWithData:self.receiveData options:NSJSONReadingAllowFragments error:&error];
    NSLog(@"%@ data\n%@",self,data);
    
    [self.callbackHandle listWtihData:data apiHandle:self];
}
@end

HotAPIHandle.h

#import "APIHandle.h"

@interface HotAPIHandle : APIHandle

@end

HotAPIHandle.m

#import "HotAPIHandle.h"

@implementation HotAPIHandle

@end

LatestAPIHandle.h

#import "APIHandle.h"

@interface LatestAPIHandle : APIHandle

@end

LatestAPIHandle.m

#import "LatestAPIHandle.h"

@implementation LatestAPIHandle

@end

ViewController.h

#import 
#import "APIInterface.h"
#import "APIParamSourceInterface.h"
#import "APICallbackInterface.h"

@interface ViewController : UIViewController

@property (nonatomic, weak) id hotAPIHandel;
@property (nonatomic, weak) id latestAPIHandel;
@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSArray *list;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    [self.hotAPIHandel loadData];
}

-(void)initUI
{
    self.title = @"最热-最新主题";
    CGRect frame = [[UIScreen mainScreen]bounds];
    self.tableView = [[UITableView alloc]initWithFrame:frame];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    NSDictionary * data =self.list[indexPath.row];
    
    return [self configureWithCell:cell data:data];
}

- (UITableViewCell *)configureWithCell:(UITableViewCell *)cell data:(NSDictionary *)data
{
    cell.textLabel.text = data[@"title"];
    cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:%@",data[@"member"][@"avatar_mini"]]]]];
    return cell;
}

-(NSDictionary *)paramSourceAPIHandle:(id)apiHandle
{
    if ([apiHandle isKindOfClass:[self.hotAPIHandel class]]) {
        return @{
                 @"URLString" : @"https://www.v2ex.com/api/topics/hot.json"
                 };
    }
    else if ([apiHandle isKindOfClass:[self.latestAPIHandel class]]) {
        return @{
                 @"URLString" : @"https://www.v2ex.com/api/topics/latest.json"
                 };
    }
    
    return nil;
}

- (void)listWtihData:(NSArray *)data apiHandle:(id)apiHandle
{
    if ([apiHandle isKindOfClass:[self.hotAPIHandel class]]) {
        self.list = data;
        [self.latestAPIHandel loadData];
    }
    else if ([apiHandle isKindOfClass:[self.latestAPIHandel class]]) {
        self.list = data;
    }
        
    [self tableViewReloadData];
}

- (void)tableViewReloadData
{
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end

AppDelegate.m

#import "HotAPIHandle.h"
#import "LatestAPIHandle.h"

@interface AppDelegate ()
@property (nonatomic, strong) HotAPIHandle *hotAPIHandle;
@property (nonatomic, strong) LatestAPIHandle *latestAPIHandle;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    ViewController *viewController = [[ViewController alloc] init];
    
    viewController.hotAPIHandel = self.hotAPIHandle;
    self.hotAPIHandle.callbackHandle = viewController;
    self.hotAPIHandle.paramSourceHandle = viewController;
    
    viewController.latestAPIHandel = self.latestAPIHandle;
    self.latestAPIHandle.callbackHandle = viewController;
    self.latestAPIHandle.paramSourceHandle = viewController;
    
    UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:viewController];
    self.window.rootViewController = navigationController;
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (HotAPIHandle *)hotAPIHandle
{
    if(!_hotAPIHandle)
    {
        _hotAPIHandle = [[HotAPIHandle alloc] init];
    }
    return _hotAPIHandle;
}

- (LatestAPIHandle *)latestAPIHandle
{
    if(!_latestAPIHandle)
    {
        _latestAPIHandle = [[LatestAPIHandle alloc] init];
    }
    return _latestAPIHandle;
}

很多朋友问我,这样设计的优势在哪里?
我觉得最大的优势就是:你很容易找到出错的代码。

后记(下面以聊家常为主,没时间没兴趣的朋友请直接忽略):

我思考过很多次,要不要继续全力推动信息。
我当然知道,因为我的水平有限,经验有限,智商有限,在大多数情况下,我推动的信息是错的。
但是,从我的人生经历来看,全力推,还是会更有益,更划算。

我们很容易看到一些“不全力推”的例子。
比如,有人会说“我今天收获很大”,然后就没有然后了,或者需要等到有人请教他,他才会继续说下去。
比如,有人会说“你这样是错的”,然后就没有然后了,或者需要等到有人请教他,他才会继续说下去。

我当然知道每个信息都得来不易。
我当然知道信息差可以产生优势。
我只是更清楚,全力去推,即使会得罪一些人,最终还是会收获更多。

退一步讲,即使因此而失去再多的机会,我好像也不在乎。因为,对我而言,推动信息才是我一生最想要的。

推它个天翻地覆。

你可能感兴趣的:(解决“单接口,多类”架构设计的回调问题)