您的位置:首页 > 移动开发

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

2016-03-12 13:08 645 查看
名称解释一下:
1、SOA是面向服务的架构,所有的第三方功能都被分别封装成服务。
2、Component 表示这个类是用于引用的,不能用于继承。

是对上一篇《使用category 为 AppDelegate 的代码分层》的改进,原文地址 /article/3715982.html

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

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


这样就可以完全独立的处理每个不同的第三方服务。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: