手机
当前位置:查字典教程网 >编程开发 >IOS开发 >iOS App开发中使用设计模式中的单例模式的实例解析
iOS App开发中使用设计模式中的单例模式的实例解析
摘要:一、单例的作用顾名思义,单例,即是在整个项目中,这个类的对象只能被初始化一次。它的这种特性,可以广泛应用于某些需要全局共享的资源中,比如管理...

一、单例的作用

顾名思义,单例,即是在整个项目中,这个类的对象只能被初始化一次。它的这种特性,可以广泛应用于某些需要全局共享的资源中,比如管理类,引擎类,也可以通过单例来实现传值。UIApplication、NSUserDefaults等都是IOS中的系统单例。

二、单例模式的两种写法

1,常用写法

#import "LGManagerCenter.h" static LGManagerCenter *managerCenter; @implementation LGManagerCenter +(LGManagerCenter *)sharedManager{ if(!managerCenter) managerCenter=[[self allocWithZone:NULL] init]; return managerCenter; } @end

2,用GCD创建单例类

#import "LGManagerCenter.h" @implementation LGManagerCenter +(LGManagerCenter *)sharedManager{ static dispatch_once_t predicate; static LGManagerCenter * managerCenter; dispatch_once(&predicate, ^{ managerCenter=[[LGManagerCenter alloc] init]; }); return managerCenter; } @end

其中dispatch_once函数只执行一次。

三、代码的优化

通过上面的方法,我们已经可以使用类方法来得到这个单例,但很多时候,项目的工程量很大,还有可能会很多开发者同时参与一个项目的开发,为了安全与管理代码的方便,也为了给不是这个单例的创作者但会用到这个单例的开发人员一些提示,我们通常会重写一些方法:

首先我们自己实现一个alloc方法:

+(instancetype)myAlloc{ return [super allocWithZone:nil]; }

将我们的单例实现方法略作修改:

+(ZYHPayManager *)sharedMamager{ static ZYHPayManager * manager; if (manager==nil) { manager=[[ZYHPayManager myAlloc]init]; } return manager; }

将一些视图实例化对象的方法重写:

+(instancetype)alloc{ NSAssert(0, @"这是一个单例对象,请使用+(ZYHPayManager *)sharedMamager方法"); return nil; } +(instancetype)allocWithZone:(struct _NSZone *)zone{ return [self alloc]; } -(id)copy{ NSLog(@"这是一个单例对象,copy将不起任何作用"); return self; } +(instancetype)new{ return [self alloc]; }

注意:这里的alloc使用了断言,让任何视图通过alloc创建对象的程序段断在此处,给程序员提示。copy方法这里只是简单的返回了原对象,并未做任何处理,打印信息给程序员提示。

【iOS App开发中使用设计模式中的单例模式的实例解析】相关文章:

深入解析iOS应用开发中对设计模式中的桥接模式的使用

实例讲解设计模式中的命令模式在iOS App开发中的运用

iOS多线程应用开发中自定义NSOperation类的实例解析

设计模式中的Memento备忘录模式的在iOS App开发中的运用

iOS App开发中使用及自定义UITableViewCell的教程

iOS开发中使用UIScrollView实现无限循环的图片浏览器

iOS开发中使用cocos2d添加触摸事件的方法

IOS设计模式之组合设计模式

iOS开发中使用Picker View实现一个点菜应用的UI示例

iOS App设计模式开发中策略模式的实现示例

精品推荐
分类导航