iOS开发设计模式那点事.docx

上传人:b****5 文档编号:7346850 上传时间:2023-01-23 格式:DOCX 页数:4 大小:19.82KB
下载 相关 举报
iOS开发设计模式那点事.docx_第1页
第1页 / 共4页
iOS开发设计模式那点事.docx_第2页
第2页 / 共4页
iOS开发设计模式那点事.docx_第3页
第3页 / 共4页
iOS开发设计模式那点事.docx_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

iOS开发设计模式那点事.docx

《iOS开发设计模式那点事.docx》由会员分享,可在线阅读,更多相关《iOS开发设计模式那点事.docx(4页珍藏版)》请在冰豆网上搜索。

iOS开发设计模式那点事.docx

iOS开发设计模式那点事

说起设计模式,感觉自己把握不了笔头,所以单拿出iOS开发中的几种常用设计模式谈一下。

单例模式(Singleton)概念:

整个应用或系统只能有该类的一个实例在iOS开发我们经常碰到只需要某类一个实例的情况,最常见的莫过于对硬件参数的访问类,比如UIAccelerometer.这个类可以帮助我们获得硬件在各个方向轴上的加速度,但是我们仅仅需要它的一个实例就够了,再多,只会浪费内存。

所以苹果提供了一个UIAccelerometer的实例化方法+sharedAccelerometer,从名字上我们也能看出此方法让整个应用共享一个UIAccelerometer实例(PS:

iOS的开放中,我们往往能从方法名中就了解这个方法的作用),它内部的如何实现我们暂且不谈,先来看看还有哪些类同样使用了单例模式。

●UIApplication类提供了+sharedAPplication方法创建和获取UIApplication单例●NSBundle类提供了+mainBunle方法获取NSBundle单例●NSFileManager类提供了+defaultManager方法创建和获得NSFileManager单例。

(PS:

有些时候我们得放弃使用单例模式,使用-init方法去实现一个新的实例,比如使用委托时)●NSNotificationCenter提供了+defaultCenter方法创建和获取NSNotificationCenter单例(PS:

该类还遵循了另一个重要的设计模式:

观察者模式)●NSUserDefaults类提供了+defaultUserDefaults方法去创建和获取NSUserDefaults单例等等,苹果的SDK中大量的遵循此设计模式,那么它的内部是如何实现的呢?

首先给大家介绍一下GCD技术,是苹果针对于多核CPU的多任务解决方案。

你不需要了解更多,只需要知道是一组基于C语言开发的API(详细内容可以看一下唐巧前辈的这篇博文:

声明一个可以新建和获取单个实例对象的方法2.声明一个static类型的类变量3.声明一个只执行一次的任务4.调用dispatch_once执行该任务指定的代码块,在该代码块中实例化上文声明的类变量5.返回在整个应用的生命周期中只会被实例化一次的变量OK,这就是iOS开发中单例模式的机制,下面我们就看看如何在实际开发中使用此模式?

(PS:

为了尽可能的突出核心内容,我们会对设计中的其他模式或内容一笔带过)假如我们需要在iOS应用中实现分层的架构设计,即我们需要把数据的持久层,展示层,和逻辑层分开。

为了突出重点,我们直接把目光转到持久层,而根据MVC的设计模式,我们又可以把持久层细分为DAO层(放置访问数据对象的四类方法)和Domain层(各种实体类,比如学生),这样就可以使用DAO层中的方法,配合实体类Domain层对数据进行清晰的增删改查。

那么我们如何设计呢?

从使用者的角度看,我们期望获得DAO层的类实例,然后调用它的增删改查四大方法。

可是这个类实例,我们似乎只需要一个就足够了,再多的话不利于管理且浪费内存。

OK,我们可以使用单例模式了,代码如下:

.h文件:

//StudentDAO.h@interfaceStudentDAO:

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"@implementationStudentDAOstaticStudentDAO*student;student2.name="Ricardo_LI";student2.studentNum="201200301102";studentDao=[[selfalloc]init];studentDao._StudentsInfo=[[NSMutaleArrayalloc]init];[studentDao._StudentsInfoaddObject:

student1];[studentDao._StudentsInfoaddObject:

student2];});returnstudentDao;}//插入的方法-(int)create:

(Student*)stu{[self._StudentsInfoaddObject:

stu];return0;}//删除的方法-(int)remove:

(Student*)stu{for(Student*sinself._StudentsInfo){if([stu.studentNumisEqual:

s.studentNum]){[self._StudentsInforemoveObject:

s]break;}}}-(int)modify......//省略不写-(NSMutaleArray)findAll......//省略不写上述例子不难理解,其中用到的Student类我这里就不给出了,只是一个含有姓名和学号属性的实体类。

观察者模式概念:

一个对象状态改变,通知正在对他进行观察的对象,这些对象根据各自要求做出相应的改变图例:

如图所示:

操作对象向被观察者对象投送消息,使得被观察者的状态得以改变,在此之前已经有观察者向被观察对象注册,订阅它的广播,现在被观察对象将自己状态发生改变的消息广播出来,观察者接收到消息各自做出应变。

OK,我们先来看看在苹果的CocoaTouch框架中有谁使用了观察者模式:

通知(notification)机制原理图如下:

如图所示,在通知机制中对某个通知感兴趣的所有对象都可以成为接受者。

首先,这些对象需要向通知中心(NSNotificationCenter)发出addObserver:

selector:

name:

object:

消息进行注册,在投送对象投送通知送给通知中心时,通知中心就会把通知广播给注册过的接受者。

所有的接受者不知道通知是谁投送的,不去关心它的细节。

投送对象和接受者是一对多的关系。

接受者如果对通知不再关注,会给通知中心发送removeObserver:

name:

Object:

消息解除注册,以后不再接受通知。

(ps:

这段话内容摘抄自关东升先生的文章)OK,我们试着去使用一下通知机制:

新建一个SingleviewProject,对项目中的文件做以下修改:

AppDelegate.m-(void)applicationDidEnterBackground:

(UIApplication*)application{[[NSNotificationCenterdefaultCenter]postNotificationName:

@"APPTerminate"object:

self];}ViewController.m////ViewController.m//TestNotification////Createdbyliwenqianon14-10-18.//Copyright(c)2014年liwenqian.Allrightsreserved.//#import"ViewController.h"@interfaceViewController()@end@implementationViewController-(void)viewDidLoad{[superviewDidLoad];//注意此处的selector有参数,要加冒号[[NSNotificationCenterdefaultCenter]addObserver:

selfselector:

@selector(doSomething:

)name:

@"APPTerminate"object:

nil];//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];[[NSNotificationCenterdefaultCenter]removeObserver:

self];//Disposeofanyresourcesthatcanberecreated.}#pragmamark-处理通知-(void)doSomething:

(NSNotification*)notification{NSLog(@"收到通知");}@end如上所示,对模版项目的两个文件的方法或整个文件做出修改,Command+R运行你的APP,再按下Home键(Command+H),会发现打印出一行收到通知的文字,如下:

在APP退到后台时,发出广播,而viewController因为时观察者,收到广播,执行doSomething方法,打印出收到广播的文字。

KVO(Key-Value-Observing)机制原理图如下:

如图所示:

该机制下观察者的注册是在被观察者的内部进行的,不同于通知机制(由观察者自己注册),需要被观察者和观察者同时实现一个协议:

NSKeyValueObserving,被观察者通过addObserver:

forKeypath:

options:

context方法注册观察者,以及要被观察的属性。

新建一个singleviewproject,同时新建一个继承自NSObject的TestWatche类:

文件结构如下图:

对文件进行如下修改:

AppDelegate.h////AppDelegate.h//TestNotification////Createdbyliwenqianon14-10-18.//Copyright(c)2014年liwenqian.Allrightsreserved.//#import<UIKit/UIKit.h>#import<CoreData/CoreData.h>#import"TestWatche.h"@interfaceAppDelegate:

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;@endAppDelegate.m对如下方法做出修改-(BOOL)application:

(UIApplication*)applicationdidFinishLaunchingWithOptions:

(NSDictionary*)launchOptions{//Overridepointforcustomizationafterapplicationlaunch.self.watcher=[TestWatchealloc];[selfaddObserver:

self.watcherforKeyPath:

@"state"options:

NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNewcontext:

@"passcontent"];self.state=@"launch";returnYES;}-(void)applicationDidEnterBackground:

(UIApplication*)application{self.state=@"backgroud";}TestWatche.m(由于继承自NSObject,而NSObject已实现了NSKeyValueObserving协议,所以不需要做声明)////TestWatche.m//TestNotification////Createdbyliwenqianon14-10-18.//Copyright(c)2014年liwenqian.Allrightsreserved.//#import"TestWatche.h"@implementationTestWatche-(void)observeValueForKeyPath:

(NSString*)keyPathofObject:

(id)objectchange:

(NSDictionary*)changecontext:

(void*)context{NSLog(@"statechange:

%@",change);}@endOK,Command+BCommand+RCommand+H看看你的应用输出了什么,如果你的操作无误的话,会显示如下内容:

委托模式个人认为委托模式大多数人解释的复杂了,其实就像是java中的接口,类可以实现或不实现协议(接口)中的方法。

通过此种方式,达到最大的解耦目的,方便项目的扩展。

不过你需要设置应用的委托对象,以确定协议中的方法为谁服务。

拿最常用的UITableViewDelegateUITableViewDataSource来举例:

实现一个页面有一个UItableView,UItableView的每一栏(cell)的数据由我们指定,那么我们该如何做呢?

苹果也自然想到了这一点,于是定义了一个接口,这个接口有许多的方法,只需要我们把要服务的对象传进去,就可以使用这些方法了,这个接口就是委托和协议。

而UITableViewDelegate和UITableViewDataSource就是专为UITableView而写的委托和协议。

用法如下:

ViewController.h////ViewController.h//RecipeBookMe////Createdbyliwenqianon14-9-10.//Copyright(c)2014年liwenqian.Allrightsreserved.//#import<UIKit/UIKit.h>@interfaceViewController:

UIViewController<UITableViewDelegate,UITableViewDataSource>@property(nonatomic,strong)IBOutletUITableView*mytableView;@endViewController.m////ViewController.m//RecipeBookMe////Createdbyliwenqianon14-9-10.//Copyright(c)2014年liwenqian.Allrightsreserved.//#import"ViewController.h"#import"DetailViewController.h"#import"Recipe.h"#import"RecipeTableCellTableViewCell.h"@interfaceViewController()@end@implementationViewController{NSArray*recipes;}@synthesizemytableView;-(void)viewDidLoad{[superviewDidLoad];//InitializetherecipesarrayRecipe*recipe1=[Recipenew];recipe1.name=@"EggBenedict";recipe1.prepTime=@"30min";recipe1.image=@"egg_benedict.jpg";recipe1.ingredients=[NSArrayarrayWithObjects:

@"2freshEnglishmuffins",@"4eggs",@"4rashersofbackbacon",@"2eggyolks",@"1tbspoflemonjuice",@"125gofbutter",@"saltandpepper",nil];Recipe*recipe2=[Recipenew];recipe2.name=@"MushroomRisotto";recipe2.prepTime=@"30min";recipe2.image=@"mushroom_risotto.jpg";recipe2.ingredients=[NSArrayarrayWithObjects:

@"1tbspdriedporcinimushrooms",@"2tbspoliveoil",@"1onion,chopped",@"2garliccloves",@"350g/12ozarboriorice",@"1.2litres/2pintshotvegetablestock",@"saltandpepper",@"25g/1ozbutter",nil];recipes=[NSArrayarrayWithObjects:

recipe1,recipe2,nil];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];}/*--------------------------------------------------------------------*///定义UITableview的栏目数量-(NSInteger)tableView:

(UITableView*)tableViewnumberOfRowsInSection:

(NSInteger)section{return[recipescount];}//定义UITableviewCell的显示内容-(UITableViewCell*)tableView:

(UITableView*)tableViewcellForRowAtIndexPath:

(NSIndexPath*)indexPath{staticNSString*CoustomerTableIdentifier=@"RecipeTableCellTableViewCell";RecipeTableCellTableViewCell*cell=(RecipeTableCellTableViewCell*)[tableViewdequeueReusableCellWithIdentifier:

CoustomerTableIdentifier];if(cell==nil){cell=[[RecipeTableCellTableViewCellalloc]initWithStyle:

UITableViewCellStyleDefaultreuseIdentifier:

CoustomerTableIdentifier];}recipe=[recipesobjectAtIndex:

indexPath.row];cell.nameLabel.text=recipe.name;cell.prepTimeLabel.text=recipe.prepTime;cell.thumbnailImageView.image=[UIImageimageNamed:

recipe.image];returncell;}//点击每一栏执行跳转时的处理-(void)prepareForSegue:

(UIStoryboardSegue*)seguesender:

(id)sender{if([segue.identifierisEqualToString:

@"showRecipeDetail"]){NSIndexPath*indexPath=nil;Recipe*recipe=nil;indexPath=[self.mytableViewindexPathForSelectedRow];recipe=[recipesobjectAtIndex:

indexPath.row];DetailViewController*destViewController=segue.destinationViewController;destViewController.recipe=[recipesobjectAtIndex:

indexPath.row];}}//定义cell的高度-(CGFloat)tableView:

(UITableView*)tableViewheightForRowAtIndexPath:

(NSIndexPath*)indexPath{return71;}/*--------------------------------------------------------------------*/@end如上所示,两条/------/注释间的方法全部来自于委托和协议。

利用委托和协议,你可以把主要精力放到逻辑业务上,将数据绑定和事件处理交给委托和协议去完成。

/

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 企业管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1