感謝此文章提供摘要: http://www.cnblogs.com/lyanet/archive/2013/01/11/2856468.htmlhtml
優缺點: http://blog.csdn.net/tayanxunhua/article/details/8250329objective-c
單利模式的7中寫法: http://cantellow.iteye.com/blog/838473sql
GCD 幾句實現單利:數據庫
.m中的寫這一步便可食用 static XSYCoreDataStackManger * xsyCoreDataManager = nil; +(XSYCoreDataStackManger *)shareInstance{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ xsyCoreDataManager = [[XSYCoreDataStackManger alloc] init]; }); return xsyCoreDataManager; }
1.單例模式的要點:app
顯然單例模式的要點有三個;一是某個類只能有一個實例;二是它必須自行建立這個實例;三是它必須自行向整個系統提供這個實例。atom
2.單例模式的優勢:spa
3.單例模式的缺點.net
一、因爲單利模式中沒有抽象層,所以單例類的擴展有很大的困難。設計
二、單例類的職責太重,在必定程度上違背了「單一職責原則」。code
三、濫用單例將帶來一些負面問題,如爲了節省資源將數據庫鏈接池對象設計爲的單例類,可能會致使共享鏈接池對象的程序過多而出現鏈接池溢出;若是實例化的對象長時間不被利用,系統會認爲是垃圾而被回收,這將致使對象狀態的丟失。
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