下面介绍代码部分。在dao组中,NoteDAO.h的代码如下: @interface NoteDAO : NSObject //保存数据列表 @property (nonatomic,strong) NSMutableArray* listData; + (NoteDAO*)sharedManager; //插入备忘录的方法 -(int) create:(Note*)model; //删除备忘录的方法 -(int) remove:(Note*)model; //修改备忘录的方法 -(int) modify:(Note*)model; //查询所有数据的方法 -(NSMutableArray*) findAll; //按照主键查询数据的方法 -(Note*) findById:(Note*)model; @end 在上述代码中,listData属性用于保存数据表中的数据,其中每一个元素都是Note对象。+ (NoteDAO*) sharedManager方法用于获得NoteDAO单例对象。在dao组中,NoteDAO.m的代码如下: @implementation NoteDAO static NoteDAO *sharedManager = nil; + (NoteDAO*)sharedManager { static dispatch_once_t once; dispatch_once(&once, ^{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *date1 = [dateFormatter dateFromString:@"2010-08-04 16:01:03"]; Note* note1 = [[Note alloc] init]; note1.date = date1; note1.content = @"Welcome to MyNotes."; NSDate *date2 = [dateFormatter dateFromString:@"2011-12-04 16:01:03"]; Note* note2 = [[Note alloc] init]; note2.date = date2; note2.content = @"欢迎使用MyNotes。"; sharedManager = [[self alloc] init]; sharedManager.listData = [[NSMutableArray alloc] init]; [sharedManager.listData addObject:note1]; [sharedManager.listData addObject:note2]; }); return sharedManager; } //插入备忘录的方法 -(int) create:(Note*)model { [self.listData addObject:model]; return 0; } //删除备忘录的方法 -(int) remove:(Note*)model { for (Note* note in self.listData) { //比较日期主键是否相等 if ([note.date isEqualToDate:model.date]){ [self.listData removeObject: note]; break; } } return 0; } //修改备忘录的方法 -(int) modify:(Note*)model { for (Note* note in self.listData) { //比较日期主键是否相等 if ([note.date isEqualToDate:model.date]){ note.content = model.content; break; } } return 0; } //查询所有数据的方法 -(NSMutableArray*) findAll { return self.listData; } //按照主键查询数据的方法 -(Note*) findById:(Note*)model { for (Note* note in self.listData) { //比较日期主键是否相等 if ([note.date isEqualToDate:model.date]){ return note; } } return nil; } @end NoteDAO也采用了单例设计模式来实现。 在domain组中,Note的代码如下,它只有两个属性——date是创建备忘录的日期,content是备忘录的内容: // //Note.h // #import <Foundation/Foundation.h> @interface Note : NSObject @property(nonatomic, strong) NSDate* date; @property(nonatomic, strong) NSString* content; @end // //Note.m // #import "Note.h" @implementation Note @end 在业务逻辑层BusinessLogicLayer中,NoteBL.h的代码如下: @interface NoteBL : NSObject //插入备忘录的方法 -(NSMutableArray*) createNote:(Note*)model; //删除备忘录的方法 -(NSMutableArray*) remove:(Note*)model; //查询所有数据的方法 -(NSMutableArray*) findAll; @end 下面是NoteBL.m中的代码: @implementation NoteBL //插入备忘录的方法 -(NSMutableArray*) createNote:(Note*)model { NoteDAO *dao = [NoteDAO sharedManager]; [dao create:model]; return [dao findAll]; } //删除备忘录的方法 -(NSMutableArray*) remove:(Note*)model { NoteDAO *dao = [NoteDAO sharedManager]; [dao remove:model]; return [dao findAll]; } //查询所有数据的方法 -(NSMutableArray*) findAll { NoteDAO *dao = [NoteDAO sharedManager]; return [dao findAll]; } @end PresentationLayer是表示层,其中的内容大家应该比较熟悉了,这里不再赘述。