为AppDelegate分层的面向服务架构的 SOAComponentAppDelegate

21 篇文章 0 订阅

名称解释一下:

1、SOA是面向服务的架构,所有的第三方功能都被分别封装成服务。

2、Component 表示这个类是用于引用的,不能用于继承。


是对上一篇《使用category 为 AppDelegate 的代码分层》的改进,原文地址 http://blog.csdn.net/teamlet/article/details/50863761  。


一、首先创建服务类,服务类是对第三方服务的封装。第三方服务包括推送、支付、统计等


1、服务举例 BaiduPushService 头文件

新创建的服务类需要添加  <UIApplicationDelegate> 协议,根据需要实现协议中的方法。这里只添加了一个作为演示。

 
//
//  BaiduPushService.h
//  SOAComponentAppDelegate
//  Version 1.0.0
//  Created by David Wang on 16/3/12.
//  Copyright © 2016年 teamlet. All rights reserved.
//


#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface BaiduPushService : NSObject  <UIApplicationDelegate>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions ;

@end
 

2、实现类

 
//
//  BaiduPushService.m
//  SOAComponentAppDelegate
//  Version 1.0.0
//  Created by David Wang on 16/3/12.
//  Copyright © 2016年 teamlet. All rights reserved.
//

#import "BaiduPushService.h"

@implementation BaiduPushService


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"BaiduPushService didFinishLaunchingWithOptions");
    return YES;
}
@end

 

二、组件类


1、 SOAComponentAppDelegate.h 头文件

定义单例方法instance()和获取服务的方法services。

 
//
//  SOAComponentAppDelegate.h
//  SOAComponentAppDelegate
//  Version 1.0.0
//  Created by David Wang on 16/3/12.
//  Copyright © 2016年 teamlet. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface SOAComponentAppDelegate : NSObject



+ (instancetype)instance ;

-(NSMutableArray*) services;

@end
 



2、SOAComponentAppDelegate.m实现

在实现类中,需要引用并注册第三方的服务类。

 
//
//  SOAComponentAppDelegate.m
//  SOAComponentAppDelegate
//  Version 1.0.0
//  Created by David Wang on 16/3/12.
//  Copyright © 2016年 teamlet. All rights reserved.
//

#import "SOAComponentAppDelegate.h"
#import "BaiduPushService.h"

@implementation SOAComponentAppDelegate
{
    NSMutableArray* allServices;
}

#pragma mark - 服务静态注册

//需要运行程序之前,手工增加根据需要的新服务

-(void)registeServices
{
    [self registeService:[[BaiduPushService alloc] init]];
    
}

#pragma mark - 获取SOAComponent单实例

+ (instancetype)instance {
    
    static SOAComponentAppDelegate *insance = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        insance = [[SOAComponentAppDelegate alloc] init];
    });
    
    
    return insance;
}

#pragma mark - 获取全部服务

-(NSMutableArray *)services
{
    
    if (!allServices) {
        allServices = [[NSMutableArray alloc]init];
        [self registeServices];
    }

    return allServices;
}

#pragma mark - 服务动态注册

-(void)registeService:(id)service
{
    if (![allServices containsObject:service])
    {
        [allServices addObject:service];
    }
    
}

@end
 



三、使用

1、AppDelegate.h 不做任何改动。

 
//
//  AppDelegate.h
//  SOAComponentAppDelegate
//  Version 1.0.0
//  Created by David Wang on 16/3/12.
//  Copyright © 2016年 teamlet. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

 



2、AppDelegate.m

导入 SOAComponentAppDelegate 和 BaiduPushService

在对应的方法里调用第三方服务中已经封装好的方法。

 
//
//  AppDelegate.m
//  SOAComponentAppDelegate
//  Version 1.0.1 补全方法
//  Created by David Wang on 16/3/12.
//  Copyright © 2016年 teamlet. All rights reserved.
//

#import "AppDelegate.h"
#import "SOAComponentAppDelegate.h"
#import "BaiduPushService.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    id<UIApplicationDelegate> service;
    for(service in [[SOAComponentAppDelegate instance] services]){
        if ([service respondsToSelector:@selector(application:didFinishLaunchingWithOptions:)]){
            [service application:application didFinishLaunchingWithOptions:launchOptions];
        }
    }
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    id<UIApplicationDelegate> service;
    for(service in [[SOAComponentAppDelegate instance] services]){
        if ([service respondsToSelector:@selector(applicationWillResignActive:)]){
            [service applicationWillResignActive:application];
        }
    }
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    id<UIApplicationDelegate> service;
    for(service in [[SOAComponentAppDelegate instance] services]){
        if ([service respondsToSelector:@selector(applicationDidEnterBackground:)]){
            [service applicationDidEnterBackground:application];
        }
    }
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    id<UIApplicationDelegate> service;
    for(service in [[SOAComponentAppDelegate instance] services]){
        if ([service respondsToSelector:@selector(applicationWillEnterForeground:)]){
            [service applicationWillEnterForeground:application];
        }
    }
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    id<UIApplicationDelegate> service;
    for(service in [[SOAComponentAppDelegate instance] services]){
        if ([service respondsToSelector:@selector(applicationDidBecomeActive:)]){
            [service applicationDidBecomeActive:application];
        }
    }
}

- (void)applicationWillTerminate:(UIApplication *)application {
    id<UIApplicationDelegate> service;
    for(service in [[SOAComponentAppDelegate instance] services]){
        if ([service respondsToSelector:@selector(applicationWillTerminate:)]){
            [service applicationWillTerminate:application];
        }
    }
}

@end
 




这样就可以完全独立的处理每个不同的第三方服务。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值