iOS 单利模式实现/优缺点

感谢此文章提供摘要: http://www.cnblogs.com/lyanet/archive/2013/01/11/2856468.html

优缺点:  http://blog.csdn.net/tayanxunhua/article/details/8250329

单利模式的7中写法: http://cantellow.iteye.com/blog/838473

GCD 几句实现单利:

.m中的写这一步即可食用

static  XSYCoreDataStackManger * xsyCoreDataManager = nil;

+(XSYCoreDataStackManger *)shareInstance{
    
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        xsyCoreDataManager = [[XSYCoreDataStackManger alloc] init];
        
    });
    
    return xsyCoreDataManager;
    
}

1.单例模式的要点:

  显然单例模式的要点有三个;一是某个类只能有一个实例;二是它必须自行创建这个实例;三是它必须自行向整个系统提供这个实例。

2.单例模式的优点:

  1.实例控制:Singleton 会阻止其他对象实例化其自己的 Singleton 对象的副本,从而确保所有对象都访问唯一实例。
  2.灵活性:因为类控制了实例化过程,所以类可以更加灵活修改实例化过程

3.单例模式的缺点

1、由于单利模式中没有抽象层,因此单例类的扩展有很大的困难。

2、单例类的职责过重,在一定程度上违背了“单一职责原则”。

3、滥用单例将带来一些负面问题,如为了节省资源将数据库连接池对象设计为的单例类,可能会导致共享连接池对象的程序过多而出现连接池溢出;如果实例化的对象长时间不被利用,系统会认为是垃圾而被回收,这将导致对象状态的丢失。

 
 
iOS中的单例模式
  在objective-c中要实现一个单例类,至少需要做以下四个步骤:
  1、为单例对象实现一个静态实例,并初始化,然后设置成nil,
  2、实现一个实例构造方法检查上面声明的静态实例是否为nil,如果是则新建并返回一个本类的实例,
  3、重写allocWithZone方法,用来保证其他人直接使用alloc和init试图获得一个新实力的时候不产生一个新实例,
  4、适当实现allocWitheZone,copyWithZone
 
下面以CenterLiShi  (数据库)为例子:
 
.h
 1 #import <Foundation/Foundation.h>
 2 #import "ModelHome.h"  //数据模型
 3 
 4 typedef enum {
 5     kRecordLISHI =1,//历史
 6     kRecordShouCang//收藏
 7 }recordType;
 8 @interface CenterLiShi : NSObject
 9 
10 +(CenterLiShi*)sharedDataCenter;
11 
12 //添加数据库
13 -(void)AddDataWithModel:(ModelHomeJing*)model andType:(recordType)type;
14 
15 //删除数据库
16 -(void)deleteDataWith:(ModelHomeJing*)model addType:(recordType)type;
17 
18 //判断是否已经包含在数据库中
19 -(BOOL)isHaddata:(ModelHomeJing*)model addtype:(recordType)type;
20 
21 //获取数据库
22 -(NSArray*)getDatashujulishi:(recordType)type;
23 
24 @end

.M

  1 #import "CenterLiShi.h"
  2 #import "FMDatabase.h"
  3 
  4 @interface CenterLiShi ()
  5 @property(nonatomic,strong)FMDatabase * database;
  6 
  7 
  8 @end
  9 
 10 @implementation CenterLiShi
 11 
 12 static CenterLiShi * centerlishi = nil; //静态实例,并且初始化
 13 
 14 +(CenterLiShi*)sharedDataCenter{//实例构造检查实力是否为nil
 15     
 16     @synchronized(self)
 17     
 18     {
 19         if (!centerlishi)
 20         
 21         {
 22             
 23             centerlishi = [[CenterLiShi alloc] init];
 24             
 25         }
 26     }
 27     
 28     return centerlishi;
 29     
 30 }
 31 
 32 +(id)allocWithZone:(struct _NSZone *)zone //重写allocWithZone方法
 33 
 34 {
 35     
 36     @synchronized(self)
 37     
 38     {
 39         if (!centerlishi)
 40         
 41         {
 42             centerlishi = [super allocWithZone:zone];
 43             
 44         }
 45     }
 46     
 47     return centerlishi;
 48     
 49 }
 50 
 51 -(id)init{  //内部写初始化方法 单利实现 
 52     if (self = [super init]) {
 53         NSString * sqlpath = [NSString stringWithFormat:@"%@/Documents/lishi.rdb",NSHomeDirectory()];
 54         
 55         _database = [[FMDatabase alloc] initWithPath:sqlpath];
 56         if (![_database open]) {
 57             NSLog(@"打开数据库失败");
 58             return nil ;
 59             
 60         }
 61         else{
 62             
 63             NSLog(@"打开数据库成功了");
 64             
 65         }
 66     
 67         
 68         NSString * sql = @"create table if not exists lishi ("
 69         "recordType varchar(32),"
 70         "jingxuanappId varchar(132),"
 71     "labMc varchar(132),"
 72     "imgDatu varchar(132),"
 73     "imgtouxiang varchar(132),"
 74     "labyonghu varchar(132),"
 75     "labjieshao varchar(132)"
 76         ")";
 77         
 78         BOOL iscuc = [_database executeUpdate:sql];
 79         
 80         if (iscuc)
 81         
 82         {
 83             NSLog(@"表格创建成功了");
 84             
 85         }else{
 86             
 87             NSLog(@"表格创建失败了");
 88             
 89         }
 90     }
 91     
 92     return self;
 93     
 94     
 95 }
 96 //
 97 -(void)AddDataWithModel:(ModelHomeJing *)model andType:(recordType)type
 98 
 99 {
100     
101     NSString * sql = @"INSERT INTO lishi(recordType,jingxuanappId,labMc,imgDatu,imgtouxiang,labyonghu,labjieshao) values(?,?,?,?,?,?,?)";
102     
103     BOOL iscus = [_database executeUpdate:sql,[NSString stringWithFormat:@"%d",type],model.jingxuanappId,model.labMc,model.imgDatu,model.imgtouxiang,model.labyonghu,model.labjieshao];
104     
105     if (iscus)
106     
107     {
108         NSLog(@"添加数据");
109     }
110     
111     else
112     
113     {
114         
115         NSLog(@"没有添加");
116         
117     }
118 }
119 
120 -(void)deleteDataWith:(ModelHomeJing *)model addType:(recordType)type
121 
122 {
123     
124     NSString * sql = @"delete from lishi where jingxuanappId=? and recordType=?";
125     
126     BOOL isd = [_database executeUpdate:sql,model.jingxuanappId,[NSString stringWithFormat:@"%i",type]];
127     
128     if (isd)
129     
130     {
131         NSLog(@"删除成功");
132         
133     }
134     
135     else
136     
137     {
138         
139         
140         NSLog(@"没有删除");
141         
142     }
143 }
144 
145 
146 -(NSArray *)getDatashujulishi:(recordType)type
147 
148 {
149     
150     NSString * sql = @"select * from lishi where recordType=?";
151     
152     FMResultSet * set = [_database executeQuery:sql,[NSString stringWithFormat:@"%i",type]];
153     
154     NSMutableArray * array = [NSMutableArray array];
155     
156     while ([set next])
157     
158     {
159         ModelHomeJing * mod =[ModelHomeJing modelWithSet:set];
160         
161         [array addObject:mod];
162     
163     }
164     
165     return array;
166     
167     
168 }
169 
170 -(BOOL)isHaddata:(ModelHomeJing *)model addtype:(recordType)type
171 
172 {
173     NSString * sql = @"select count(*) from lishi where jingxuanappId=? and recordType=?";
174     FMResultSet * set = [self.database executeQuery:sql,model.jingxuanappId,[NSString stringWithFormat:@"%i",type]];
175     
176     int count=0;
177     
178     if ([set next])
179     
180     {
181         count = [set intForColumnIndex:0];
182         
183     }
184     
185     return count;
186     
187     
188 }
189 @end
 
原文地址:https://www.cnblogs.com/xujiahui/p/6018466.html