名称解释一下:
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
//
// 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
在实现类中,需要引用并注册第三方的服务类。
// // 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
导入 SOAComponentAppDelegate 和 BaiduPushService
在对应的方法里调用第三方服务中已经封装好的方法。
// // AppDelegate.m // SOAComponentAppDelegate // Version 1.0.0 // 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 { } - (void)applicationDidEnterBackground:(UIApplication *)application { } - (void)applicationWillEnterForeground:(UIApplication *)application { } - (void)applicationDidBecomeActive:(UIApplication *)application { } - (void)applicationWillTerminate:(UIApplication *)application { } @end
为AppDelegate分层的面向服务架构的 SOAComponentAppDelegate
原文:http://blog.csdn.net/teamlet/article/details/50864893