为AppDelegate分层的面向服务架构的 SOAComponentAppDelegate (AppDelegate 回调分层)

来源:互联网 发布:ipad好用软件 编辑:程序博客网 时间:2024/06/13 20:17

http://blog.csdn.net/teamlet/article/details/50864893


名称解释一下:

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

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


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


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


1、服务举例 BaiduPushService 头文件

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

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.    
  2. //  
  3. //  BaiduPushService.h  
  4. //  SOAComponentAppDelegate  
  5. //  Version 1.0.0  
  6. //  Created by David Wang on 16/3/12.  
  7. //  Copyright © 2016年 teamlet. All rights reserved.  
  8. //  
  9.   
  10.   
  11. #import <Foundation/Foundation.h>  
  12. #import <UIKit/UIKit.h>  
  13.   
  14. @interface BaiduPushService : NSObject  <UIApplicationDelegate>  
  15.   
  16. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions ;  
  17.   
  18. @end  
  19.    

2、实现类

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.    
  2. //  
  3. //  BaiduPushService.m  
  4. //  SOAComponentAppDelegate  
  5. //  <span style="font-family: Arial, Helvetica, sans-serif;">Version 1.0.0</span>  
  6. //  Created by David Wang on 16/3/12.  
  7. //  Copyright © 2016年 teamlet. All rights reserved.  
  8. //  
  9.   
  10. #import "BaiduPushService.h"  
  11.   
  12. @implementation BaiduPushService  
  13.   
  14.   
  15. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  16. {  
  17.     NSLog(@"BaiduPushService didFinishLaunchingWithOptions");  
  18.     return YES;  
  19. }  
  20. @end  
  21.   
  22.    

二、组件类


1、 SOAComponentAppDelegate.h 头文件

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

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.    
  2. //  
  3. //  SOAComponentAppDelegate.h  
  4. //  SOAComponentAppDelegate  
  5. //  Version 1.0.0  
  6. //  Created by David Wang on 16/3/12.  
  7. //  Copyright © 2016年 teamlet. All rights reserved.  
  8. //  
  9.   
  10. #import <Foundation/Foundation.h>  
  11.   
  12. @interface SOAComponentAppDelegate : NSObject  
  13.   
  14.   
  15.   
  16. + (instancetype)instance ;  
  17.   
  18. -(NSMutableArray*) services;  
  19.   
  20. @end  
  21.    



2、SOAComponentAppDelegate.m实现

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

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.    
  2. //  
  3. //  SOAComponentAppDelegate.m  
  4. //  SOAComponentAppDelegate  
  5. //  Version 1.0.0  
  6. //  Created by David Wang on 16/3/12.  
  7. //  Copyright © 2016年 teamlet. All rights reserved.  
  8. //  
  9.   
  10. #import "SOAComponentAppDelegate.h"  
  11. #import "BaiduPushService.h"  
  12.   
  13. @implementation SOAComponentAppDelegate  
  14. {  
  15.     NSMutableArray* allServices;  
  16. }  
  17.   
  18. #pragma mark - 服务静态注册  
  19.   
  20. //需要运行程序之前,手工增加根据需要的新服务  
  21.   
  22. -(void)registeServices  
  23. {  
  24.     [self registeService:[[BaiduPushService alloc] init]];  
  25.       
  26. }  
  27.   
  28. #pragma mark - 获取SOAComponent单实例  
  29.   
  30. + (instancetype)instance {  
  31.       
  32.     static SOAComponentAppDelegate *insance = nil;  
  33.     static dispatch_once_t once;  
  34.     dispatch_once(&once, ^{  
  35.         insance = [[SOAComponentAppDelegate alloc] init];  
  36.     });  
  37.       
  38.       
  39.     return insance;  
  40. }  
  41.   
  42. #pragma mark - 获取全部服务  
  43.   
  44. -(NSMutableArray *)services  
  45. {  
  46.       
  47.     if (!allServices) {  
  48.         allServices = [[NSMutableArray alloc]init];  
  49.         [self registeServices];  
  50.     }  
  51.   
  52.     return allServices;  
  53. }  
  54.   
  55. #pragma mark - 服务动态注册  
  56.   
  57. -(void)registeService:(id)service  
  58. {  
  59.     if (![allServices containsObject:service])  
  60.     {  
  61.         [allServices addObject:service];  
  62.     }  
  63.       
  64. }  
  65.   
  66. @end  
  67.    



三、使用

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

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.    
  2. //  
  3. //  AppDelegate.h  
  4. //  SOAComponentAppDelegate  
  5. //  Version 1.0.0  
  6. //  Created by David Wang on 16/3/12.  
  7. //  Copyright © 2016年 teamlet. All rights reserved.  
  8. //  
  9.   
  10. #import <UIKit/UIKit.h>  
  11.   
  12. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  13.   
  14. @property (strongnonatomicUIWindow *window;  
  15.   
  16.   
  17. @end  
  18.   
  19.    



2、AppDelegate.m

导入 SOAComponentAppDelegate 和 BaiduPushService

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

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.    
  2. //  
  3. //  AppDelegate.m  
  4. //  SOAComponentAppDelegate  
  5. //  Version 1.0.1 补全方法  
  6. //  Created by David Wang on 16/3/12.  
  7. //  Copyright © 2016年 teamlet. All rights reserved.  
  8. //  
  9.   
  10. #import "AppDelegate.h"  
  11. #import "SOAComponentAppDelegate.h"  
  12. #import "BaiduPushService.h"  
  13.   
  14. @interface AppDelegate ()  
  15.   
  16. @end  
  17.   
  18. @implementation AppDelegate  
  19.   
  20.   
  21. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  22.       
  23.     id<UIApplicationDelegate> service;  
  24.     for(service in [[SOAComponentAppDelegate instance] services]){  
  25.         if ([service respondsToSelector:@selector(application:didFinishLaunchingWithOptions:)]){  
  26.             [service application:application didFinishLaunchingWithOptions:launchOptions];  
  27.         }  
  28.     }  
  29.       
  30.     return YES;  
  31. }  
  32.   
  33. - (void)applicationWillResignActive:(UIApplication *)application {  
  34.     id<UIApplicationDelegate> service;  
  35.     for(service in [[SOAComponentAppDelegate instance] services]){  
  36.         if ([service respondsToSelector:@selector(applicationWillResignActive:)]){  
  37.             [service applicationWillResignActive:application];  
  38.         }  
  39.     }  
  40. }  
  41.   
  42. - (void)applicationDidEnterBackground:(UIApplication *)application {  
  43.     id<UIApplicationDelegate> service;  
  44.     for(service in [[SOAComponentAppDelegate instance] services]){  
  45.         if ([service respondsToSelector:@selector(applicationDidEnterBackground:)]){  
  46.             [service applicationDidEnterBackground:application];  
  47.         }  
  48.     }  
  49. }  
  50.   
  51. - (void)applicationWillEnterForeground:(UIApplication *)application {  
  52.     id<UIApplicationDelegate> service;  
  53.     for(service in [[SOAComponentAppDelegate instance] services]){  
  54.         if ([service respondsToSelector:@selector(applicationWillEnterForeground:)]){  
  55.             [service applicationWillEnterForeground:application];  
  56.         }  
  57.     }  
  58. }  
  59.   
  60. - (void)applicationDidBecomeActive:(UIApplication *)application {  
  61.     id<UIApplicationDelegate> service;  
  62.     for(service in [[SOAComponentAppDelegate instance] services]){  
  63.         if ([service respondsToSelector:@selector(applicationDidBecomeActive:)]){  
  64.             [service applicationDidBecomeActive:application];  
  65.         }  
  66.     }  
  67. }  
  68.   
  69. - (void)applicationWillTerminate:(UIApplication *)application {  
  70.     id<UIApplicationDelegate> service;  
  71.     for(service in [[SOAComponentAppDelegate instance] services]){  
  72.         if ([service respondsToSelector:@selector(applicationWillTerminate:)]){  
  73.             [service applicationWillTerminate:application];  
  74.         }  
  75.     }  
  76. }  
  77.   
  78. @end  
  79.    




这样就可以完全独立的处理每个不同的第三方服务。
0 0
原创粉丝点击