美文网首页GeekBand极客班-iOS开发关于iOS 那点事IOS开发
解决“单接口,多类”架构设计的回调问题

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

作者: 利炳根 | 来源:发表于2015-09-07 01:28 被阅读540次

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

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

最热主题 最新主题

工程目录:

工程目录

代码:

APIInterface.h

#ifndef CallBack_APIInterface_h
#define CallBack_APIInterface_h
@protocol APIInterface <NSObject>

- (void)loadData;
@end
#endif

APIParamSourceInterface.h

#import "APIInterface.h"

#ifndef CallBack_APIParamSourceInterface_h
#define CallBack_APIParamSourceInterface_h
@protocol APIParamSourceInterface <NSObject>

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

APICallbackInterface.h

#import "APIInterface.h"

#ifndef CallBack_APICallbackInterface_h
#define CallBack_APICallbackInterface_h
@protocol APICallbackInterface <NSObject>

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

APIHandle.h

#import <Foundation/Foundation.h>
#import "APIInterface.h"
#import "APIParamSourceInterface.h"
#import "APICallbackInterface.h"

@interface APIHandle : NSObject<APIInterface>
@property (nonatomic, strong) NSMutableData *receiveData;
@property (nonatomic, weak) id<APIParamSourceInterface> paramSourceHandle;
@property (nonatomic, weak) id<APICallbackInterface> 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 <UIKit/UIKit.h>
#import "APIInterface.h"
#import "APIParamSourceInterface.h"
#import "APICallbackInterface.h"

@interface ViewController : UIViewController<APIParamSourceInterface,APICallbackInterface>

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

ViewController.m

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@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<APIInterface>)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<APIInterface>)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;
}

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

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

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

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

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

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

推它个天翻地覆。

相关文章

  • 依赖管理

    不等心情,一点一点强行开始 上面博文《解决“单接口,多类”架构设计的回调问题》的源码已经上传Github:http...

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

    全力推动信息,而非照顾他人感受 Github 源码:https://github.com/libinggen/Ca...

  • 单接口,多类 的架构设计

    信息胜于事实 APP架构设计有三种方式,我们以这个页面中间圆形按钮的页面跳转为例: 不管是什么设计,我们都会有一堆...

  • Java回调深入理解

    1 接口回调 1.1 接口回调概念 什么是接口回调接口回调是指:可以把使用某一接口的类创建的对象的引用赋给该接口声...

  • Android Module之间数据传递

    方法一:使用接口回调 (1)在子module创建回调接口(参数可变) (2)在子module 实现类设置接口回调 ...

  • java回调函数

    利用接口来实现回调,即在调用回调函数的类中实现接口,并实现接口中的方法即回调的方法,被调用类中存在接口的熟悉,并将...

  • Java之代理设计模式

    接口实现回调 ,代理设计模式   Read类 Chat类 都需要设置字体颜⾊和⼤⼩ Setting类就能完成这...

  • Java到Android逐步理解接口回调

    一,什么是接口回调: 接口回调(自己理解):例如A.class这个类实现了某一接口(C),我们可以把创建A类对象的...

  • 为什么说过滤器Filter由回调实现

    回调(个人理解):类A中任意方法中调用了类B的方法,而B方法又调用了A中某回调方法; 加了接口的说法:回调接口的实...

  • android回调机制(附源码)

    1.回调的用处 1.要实现一个回调,一般需要三个类: 定义接口类、实现接口类、设置触发接口类;2.在android...

网友评论

  • brave723:HotAPIHandle 和 LatestAPIHandle 没看到他们做什么功能啊!

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

本文链接:https://www.haomeiwen.com/subject/nokycttx.html