题外话:提及设计模式,感受本身把握不了笔头,因此单拿出iOS开发中的几种经常使用设计模式谈一下java
概念:整个应用或系统只能有该类的一个实例设计模式
在iOS开发咱们常常碰到只须要某类一个实例的状况,最多见的莫过于对硬件参数的访问类,好比UIAccelerometer.这个类能够帮助咱们得到硬件在各个方向轴上的加速度,可是咱们仅仅须要它的一个实例就够了,再多,只会浪费内存。架构
因此苹果提供了一个UIAccelerometer的实例化方法+sharedAccelerometer,从名字上咱们也能看出此方法让整个应用共享一个UIAccelerometer实例(PS:iOS 的开放中,咱们每每能从方法名中就了解这个方法的做用),它内部的如何实现咱们暂且不谈,先来看看还有哪些类一样使用了单例模式。app
等等,苹果的SDK中大量的遵循此设计模式,那么它的内部是如何实现的呢?框架
首先给你们介绍一下GCD技术,是苹果针对于多核CPU的多任务解决方案。你不须要了解更多,只须要知道是一组基于C语言开发的API(详细内容能够看一下唐巧前辈的这篇博文:http://blog.devtang.com/blog/2012/02/22/use-gcd/ )。GCD提供了一个dispatch_once函数,这个函数的做用就是保证block(代码块:暂时理解为一个跟函数相近的东西,具体能够参照这篇文章:http://blog.csdn.net/enuola/article/details/8674063 )里的语句在整个应用的生命周期里只执行一次。ide
OK,接下来就给出一个使用了单例模式新建和获取实例的类模版,代码以下:函数
//Singleton.h @interface Singleton : NSObject + (Singleton *)sharedSingleton; <1> @end /***************************************************************/ //Singleton.m #import "Singleton.h" @implementation Singleton static Singleton *sharedSingleton = nil;<2> + (Singleton *)sharedSingleton{ static dispatch_once_t once;<3> dispatch_once(&once,^{ sharedSingleton = [[self alloc] init];<4> //dosometing }); return sharedSingleton;<5> }
上述代码中有5小步,解释以下:post
OK,这就是iOS开发中单例模式的机制,下面咱们就看看如何在实际开发中使用此模式?(PS:为了尽量的突出核心内容,咱们会对设计中的其余模式或内容一笔带过)ui
假如咱们须要在iOS应用中实现分层的架构设计,即咱们须要把数据的持久层,展现层,和逻辑层分开。为了突出重点,咱们直接把目光转到持久层,而根据MVC的设计模式,咱们又能够把持久层细分为DAO层(放置访问数据对象的四类方法)和Domain层(各类实体类,好比学生),这样就可使用DAO层中的方法,配合实体类Domain层对数据进行清晰的增删改查。那么咱们如何设计呢?atom
从使用者的角度看,咱们指望得到DAO层的类实例,而后调用它的增删改查四大方法。但是这个类实例,咱们彷佛只须要一个就足够了,再多的话不利于管理且浪费内存。OK,咱们可使用单例模式了,代码以下:
.h文件:
//StudentDAO.h @interface StudentDAO:NSObject @property (nonatomic,strong) NSMutaleArray *StudentsInfo; + (StudentDAO *)sharedStudentDAO; -(int) create:(Student*)student; -(int) remove:(Student*)student; -(int) modify:(Student*)student; -(NSMutaleArray) findAll; @end
.m文件:
//StudentDAO.m #import "StudentDAO.h" #import "Student.h" @implementation StudentDAO static StudentDAO *studentDao = nil; + (StudentDAO)sharedStudentDAO{ static dispatch_once_t once; dispatch_once(&once,^{ Student *student1 = [[Student alloc]init]; student1.name = "MexiQQ"; student1.studentNum = "201200301101"; Student *student2 = [[Student alloc]init]; student2.name = "Ricardo_LI"; student2.studentNum = "201200301102"; studentDao = [[self alloc] init]; studentDao._StudentsInfo = [[NSMutaleArray alloc]init]; [studentDao._StudentsInfo addObject:student1]; [studentDao._StudentsInfo addObject:student2]; }); return studentDao; } //插入的方法 -(int)create:(Student*)stu{ [self._StudentsInfo addObject:stu]; return 0; } //删除的方法 -(int)remove:(Student*)stu{ for(Student* s in self._StudentsInfo){ if([stu.studentNum isEqual:s.studentNum]){ [self._StudentsInfo removeObject:s] break; } } } -(int)modify...... //省略不写 -(NSMutaleArray)findAll...... //省略不写
上述例子不难理解,其中用到的Student类我这里就不给出了,只是一个含有姓名和学号属性的实体类。
概念:一个对象状态改变,通知正在对他进行观察的对象,这些对象根据各自要求作出相应的改变
图例:
如图所示:操做对象向被观察者对象投送消息,使得被观察者的状态得以改变,在此以前已经有观察者向被观察对象注册,订阅它的广播,如今被观察对象将本身状态发生改变的消息广播出来,观察者接收到消息各自作出应变。
OK,咱们先来看看在苹果的Cocoa Touch框架中有谁使用了观察者模式:
原理图以下:
如图所示,在通知机制中对某个通知感兴趣的全部对象均可以成为接受者。首先,这些对象须要向通知中心(NSNotificationCenter)发出addObserver:selector:name:object:消息进行注册,在投送对象投送通知送给通知中心时,通知中心就会把通知广播给注册过的接受者。全部的接受者不知道通知是谁投送的,不去关心它的细节。投送对象和接受者是一对多的关系。接受者若是对通知再也不关注,会给通知中心发送removeObserver:name:Object:消息解除注册,之后再也不接受通知。
(ps:这段话内容摘抄自关东升先生的文章)
OK,咱们试着去使用一下通知机制:
新建一个Single view Project,对项目中的文件作如下修改:
AppDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application { [[NSNotificationCenter defaultCenter]postNotificationName:@"APPTerminate" object:self]; }
ViewController.m
// // ViewController.m // TestNotification // // Created by liwenqian on 14-10-18. // Copyright (c) 2014年 liwenqian. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //注意此处的selector有参数,要加冒号 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doSomething:) name:@"APPTerminate" object:nil]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; [[NSNotificationCenter defaultCenter]removeObserver:self]; // Dispose of any resources that can be recreated. } #pragma mark -处理通知 -(void)doSomething:(NSNotification*)notification{ NSLog(@"收到通知"); } @end
如上所示,对模版项目的两个文件的方法或整个文件作出修改,Command+R运行你的APP,再按下Home键(Command+H),会发现打印出一行收到通知的文字,以下:
在APP退到后台时,发出广播,而viewController由于时观察者,收到广播,执行doSomething方法,打印出收到广播的文字。
原理图以下:
如图所示:
该机制下观察者的注册是在被观察者的内部进行的,不一样于通知机制(由观察者本身注册),须要被观察者和观察者同时实现一个协议:NSKeyValueObserving,被观察者经过addObserver:forKeypath:options:context方法注册观察者,以及要被观察的属性。
新建一个single view project,同时新建一个继承自NSObject的TestWatche类:文件结构以下图:
对文件进行以下修改:
AppDelegate.h
// // AppDelegate.h // TestNotification // // Created by liwenqian on 14-10-18. // Copyright (c) 2014年 liwenqian. All rights reserved. // #import <UIKit/UIKit.h> #import <CoreData/CoreData.h> #import "TestWatche.h" @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; @property (strong,nonatomic) NSString *state; @property (strong,nonatomic) TestWatche *watcher; - (void)saveContext; - (NSURL *)applicationDocumentsDirectory; @end
AppDelegate.m 对以下方法作出修改
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.watcher = [TestWatche alloc]; [self addObserver:self.watcher forKeyPath:@"state" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"pass content"]; self.state = @"launch"; return YES; } - (void)applicationDidEnterBackground:(UIApplication *)application { self.state = @"backgroud"; }
TestWatche.m(因为继承自NSObject,而NSObject已实现了NSKeyValueObserving协议,因此不须要作声明)
// // TestWatche.m // TestNotification // // Created by liwenqian on 14-10-18. // Copyright (c) 2014年 liwenqian. All rights reserved. // #import "TestWatche.h" @implementation TestWatche -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ NSLog(@"state change:%@",change); } @end
OK,Command+B Command+R Command+H看看你的应用输出了什么,若是你的操做无误的话,会显示以下内容:
我的认为委托模式大多数人解释的复杂了,其实就像是java中的接口,类能够实现或不实现协议(接口)中的方法。经过此种方式,达到最大的解耦目的,方便项目的扩展。不过你须要设置应用的委托对象,以肯定协议中的方法为谁服务。
拿最经常使用的UITableViewDelegate UITableViewDataSource来举例:
实现一个页面有一个UItableView,UItableView的每一栏(cell)的数据由咱们指定,那么咱们该如何作呢?苹果也天然想到了这一点,因而定义了一个接口,这个接口有许多的方法,只须要咱们把要服务的对象传进去,就可使用这些方法了,这个接口就是委托和协议。而UITableViewDelegate 和 UITableViewDataSource 就是专为UITableView而写的委托和协议。用法以下:
ViewController.h
// // ViewController.h // RecipeBookMe // // Created by liwenqian on 14-9-10. // Copyright (c) 2014年 liwenqian. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> @property (nonatomic, strong) IBOutlet UITableView *mytableView; @end
ViewController.m
// // ViewController.m // RecipeBookMe // // Created by liwenqian on 14-9-10. // Copyright (c) 2014年 liwenqian. All rights reserved. // #import "ViewController.h" #import "DetailViewController.h" #import "Recipe.h" #import "RecipeTableCellTableViewCell.h" @interface ViewController () @end @implementation ViewController{ NSArray *recipes; } @synthesize mytableView; - (void)viewDidLoad { [super viewDidLoad]; // Initialize the recipes array Recipe *recipe1 = [Recipe new]; recipe1.name = @"Egg Benedict"; recipe1.prepTime = @"30 min"; recipe1.image = @"egg_benedict.jpg"; recipe1.ingredients = [NSArray arrayWithObjects:@"2 fresh English muffins", @"4 eggs", @"4 rashers of back bacon", @"2 egg yolks", @"1 tbsp of lemon juice", @"125 g of butter", @"salt and pepper", nil]; Recipe *recipe2 = [Recipe new]; recipe2.name = @"Mushroom Risotto"; recipe2.prepTime = @"30 min"; recipe2.image = @"mushroom_risotto.jpg"; recipe2.ingredients = [NSArray arrayWithObjects:@"1 tbsp dried porcini mushrooms", @"2 tbsp olive oil", @"1 onion, chopped", @"2 garlic cloves", @"350g/12oz arborio rice", @"1.2 litres/2 pints hot vegetable stock", @"salt and pepper", @"25g/1oz butter", nil]; recipes = [NSArray arrayWithObjects:recipe1, recipe2, nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } /*--------------------------------------------------------------------*/ //定义UITableview的栏目数量 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [recipes count]; } //定义UITableviewCell的显示内容 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CoustomerTableIdentifier = @"RecipeTableCellTableViewCell"; RecipeTableCellTableViewCell *cell =(RecipeTableCellTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CoustomerTableIdentifier]; if (cell == nil) { cell = [[RecipeTableCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CoustomerTableIdentifier]; } recipe = [recipes objectAtIndex:indexPath.row]; cell.nameLabel.text = recipe.name; cell.prepTimeLabel.text= recipe.prepTime; cell.thumbnailImageView.image = [UIImage imageNamed:recipe.image]; return cell; } //点击每一栏执行跳转时的处理 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"showRecipeDetail"]) { NSIndexPath *indexPath = nil; Recipe *recipe = nil; indexPath = [self.mytableView indexPathForSelectedRow]; recipe = [recipes objectAtIndex:indexPath.row]; DetailViewController *destViewController = segue.destinationViewController; destViewController.recipe = [recipes objectAtIndex:indexPath.row]; } } //定义cell的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 71; } /*--------------------------------------------------------------------*/ @end
如上所示,两条/------/注释间的方法所有来自于委托和协议。利用委托和协议,你能够把主要精力放到逻辑业务上,将数据绑定和事件处理交给委托和协议去完成。