用工厂模式处理数据库操作(object c)

来源:互联网 发布:开源的时序数据库 编辑:程序博客网 时间:2024/05/03 08:05

代码比较的多,不过注释也比较详细,不明白的可以留言给我。这个我工作中代码优化时写的,呵呵,代码没法带出来,我凭记忆用文本手打的,错误难免,有什么建议和意见请不吝赐教。
下面的代码有这些文件:

TableDefine.h //数据库表的表名和列名的定义
Contact.h //表结构对象
Contact.m
DBOperate.h //数据操作基类
DBOperate.m
RelationShip.h //基类和子类的关系表
RelationShip.m
ContactDBOperate.h //其中的一个子类
ContactDBOperate.m


//TableDefine.h
//定义表名,统一使用

#define T_CONTACT @"contact"
//定义表的字段名统一使用,避免写错,后期更改也比较统一方便
#define F_CONTACTID@"contactId"
#define F_NAME@"name"
#define F_SEX@"sex"

//定义其他的表


//Contact.h
#import <Foundation/Foundation.h>
@interface Contact:NSObject
{
NSString* contactId;
NSString* name;
NSString* sex;
}
@property (nonatomic,retain) NSString* contactId;
@property (nonatomic,retain) NSString* name;
@property (nonatomic,retain) NSString* sex;
@end


//Contact,m
#import "Contact.h"
@implement Contact
@synthesized contactId,name,sex;


-(id)init
{
if(!(self=[self super]))
{
self = [[Contact alloc] init];
}
return self;
}


-(void)dealloc
{
[contactId release];
[name release];
[sex release];
[super dealloc];
}
@end


//基类DBOperate.h
//DBOperate.m
#import <Foundation/Foundation.h>
#impoet "TableDefine.h"

typedef enum
{
     INSERT = 0,
     UPDATE,
     DELETE,
     QUERY,
 
}OPERATETYPE;

@interface DBOperate:NSObject
{
RelationShip * relationShip;
}

//可以在用一个文件包一层,使对外的接口只有一个,隐藏四个动作。
//创建单例只需初始化一次,方面给外界调用
-(id)shareInstance;
-(void)releaseInstance;

//这四个是给子类继承的,子类中实现,基类中的方法是空的
-(BOOL)insert:(id)object;
-(BOOL)update:(id)object;
-(BOOL)delete:(NSMutableDictionary*)param;
-(BOOL)query:(NSMutableDictionary*)param;


//这个方法是对外的接口,传入一个表名,操作动作类型,还有条件参数
//最有讲究的就是参数param,如果是多个参数,则可以为字典,将多参都放到字典中,
//表中的字段为键值,对应的条件为value
//paramd的传值自由控制
-(id)dbOperate:(NSString*)tableName operateType:(OPERATETYPE)type conditions:(id)param;

@end



//DBOperate.m
#import  "DBOperate.h"
static DBOperate * instance;

@implement DBOperate

-(id)shareInstance
{
@synchronized(self)
{
if(!(self=[self super]))
{
self = [[DBOperae alloc] init];
}
[self relationShipInit];
}
return self;
}
-(void)releaseInstance
{
if(self)
{
[self release];
self = nil;
}
}


-(void)relationShipInit
{
//数据库基类与子类关联类
relationShip = [[RelationShip alloc] init];
}
-(id)dbOperate:(NSString*)tableName operateType:(OPERATETYPE)type conditions:(id)param
{
//得到子类对象,进入子类的操作
DBOperate *dboperate = (DBOperate*)[relationShip getTableClass:tableName];
switch(type)
{
case INSERT
{
BOOL isSuccess= [dboperate insert:param];
return [NSNumber numberWithbool:isSuccess];
}
case UPDATE
{
BOOL isSuccess= [dboperate update:param];
return [NSNumber numberWithbool:isSuccess];
}
case DELETE
{
BOOL isSuccess= [dboperate delete:param];
return [NSNumber numberWithbool:isSuccess];
}
case QUERY
{
//返回NSMutableArray,里面存放你搜到的对象
NSMutableArray* array=(NSMutableArray*)[dboperate query:param];
return array;
}
}
return nil;
}


-(BOOL)insert:(id)object
{


}


-(BOOL)update:(id)object
{
}


-(BOOL)delete:(NSMutableDictionary*)param
{
}


-(BOOL)query:(NSMutableDictionary*)param
{
}


-(void)dealloc
{
[relationShip release];
[super dealloc];
}
@end


////RelationShip.h
#import <Foundation/Foundation.h>
@class ContactDBOperate;
@interface RelationShip:NSObject
{
ContactDBOperate *contactDBOperate;
NSMutableDictionary* tableDic;
}
-(id)getTableClass:(NSString*)tableName;
@end


////RelationShip.m
#import "RelationShip.h"
#improt "ContactDBOperate.h"
@implement RelationShip


-(id)init
{
if(!(self=[self super]))
{
self =[[RelationShip alloc]init];
//初始化各个子类,填入tableDic中
contactDBOperate = [[ContactDBOperate alloc] init];
tableDic = [[NSMutableDictionary alloc] init];

[tableDic addObject:contactDBOperate byKey:T_CONTACT];
}
return self;
}


-(id)getTableClass:(NSString*)tableName
{
if(tableName)
{
id object =[tableDic getObjectByKey:tableName];
}
return object;
}
-(void)dealloc
{
[contactDBOperate release];
[tableDic release];
[super dealloc];
}
@end


//ContactDBOperate.h
#import <Foundation/Foundation.h>
//注意继承父类
@interface ContactDBOperate:DBOperate
{


}

@end

//ContactDBOperate.m
@implement ContactDBOperate


-(BOOL)insert:(id)object
{
Contact *contact = (Contact*)object;
//这里执行数据库的插入操作,我只写sql语句的组合
NSMutableString *sqlStr = [NSMutableString stringWithFormat:@"insert into  %@ (",T_CONTACT];
[sqlStr stringFormat:@" %@, %@, %@)",F_CONTACTID,F_NAME,F_SEX];
[sqlStr stringFormat:@"values (?,?,?)",contact.contactId,contact.name,contact.sex];

//...




-(BOOL)update:(id)object
{
Contact *contact = (Contact*)object;
//这里执行数据库的插入操作,我只写sql语句的组合
NSMutableString *sqlStr = [NSMutableString stringWithFormat:@"update %@ set (",T_CONTACT];
[sqlStr stringFormat:@" %@, %@)",F_NAME,F_SEX];
[sqlStr stringFormat:@" values (?,?)",contact.name,contact.sex];
[sqlStr stringFormat:@"where F_CONTACTID = '%@'",contact.contactId];

//...
}


-(BOOL)delete:(NSMutableDictionary*)param
{
//没写完,有时间加上
}


-(BOOL)query:(NSMutableDictionary*)param
{
//没写完,有时间加上,比较重要,比insert和update难
}


@end

原创粉丝点击