为AppDelegate分层的面向服务架构的 SOAComponentAppDelegate
2016-03-12 13:08
645 查看
名称解释一下:
1、SOA是面向服务的架构,所有的第三方功能都被分别封装成服务。
2、Component 表示这个类是用于引用的,不能用于继承。
是对上一篇《使用category 为 AppDelegate 的代码分层》的改进,原文地址 /article/3715982.html 。
一、首先创建服务类,服务类是对第三方服务的封装。第三方服务包括推送、支付、统计等
1、服务举例 BaiduPushService 头文件
新创建的服务类需要添加 <UIApplicationDelegate> 协议,根据需要实现协议中的方法。这里只添加了一个作为演示。
2、实现类
二、组件类
1、 SOAComponentAppDelegate.h 头文件
定义单例方法instance()和获取服务的方法services。
2、SOAComponentAppDelegate.m实现
在实现类中,需要引用并注册第三方的服务类。
三、使用
1、AppDelegate.h 不做任何改动。
2、AppDelegate.m
导入 SOAComponentAppDelegate 和 BaiduPushService
在对应的方法里调用第三方服务中已经封装好的方法。
这样就可以完全独立的处理每个不同的第三方服务。
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
这样就可以完全独立的处理每个不同的第三方服务。
相关文章推荐
- 从开发者角度解析 Android N 新特性!
- 从开发者角度解析 Android N 新特性!
- iOS开发--正则表达式
- iOS中.PCH文件的配置及使用
- cocos2d-x 2.2.3 之菜单分析(1)
- Android 5.1编译指令
- SQLITE Error inserting 或者 android.database.sqlite.SQLiteConstraintException: error code 19
- android studio 重启adb
- iOS注册远程推送消息证书后提示此证书签发者无效的解决办法
- iOS注册远程推送消息证书后提示此证书签发者无效的解决办法
- iOS注册远程推送消息证书后提示此证书签发者无效的解决办法
- Android ADB无线连接 调试
- iOS 开发--添加工程
- Android ButterKnife注解框架
- Eclips运行cocos-js报错Multiple dex files define Lcom/chukong/cocosplay/client/CocosPlayClient;
- [Android开发]Mac下NDK开发(JNI)
- android中gson与jackson比较
- Android中的USB中的UsbAccessory和UsbDevice的区别
- IOS开发随笔2
- 从C/C++到Objective-C(四)--- 属性的使用