Oh!Coder

Coding Life

Objective C--抽象工厂模式

| Comments

今天和大家分享的一个模式叫抽象工厂模式。呃,这个模式第一眼看上去还是有点儿小复杂。不过一开始,不准备直接把原生的类结构图呈现出来,而是先从一个简单的数据库访问的例子开始。这个例子的场景是公司根据使用不同数据库为基准对部门和人员进行整合,最终目的是为了降低对不同类型数据库对不同部门以及使用人员三者之间的耦合。

好的,还是先向大家展示一下抽象工厂模式的定义,什么是抽象工厂模式呢?

抽象工厂模式(Abstract Factory),提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

其实,我个人的理解是,抽象工厂模式顾名思义,就是抽象出一个接口层,然后结合工厂模式。呵呵,听起来跟没说一样,好吧,还是先给大家看一下数据库访问的类结构图吧。

pic

好的,简单分析一下上面这张类结构图,这张图中有三个独立的模块儿,一个是IFactory接口,以不同数据库为划分原则对部门进行抽象,一个是对访问数据库的不同部门,还有一个是对数据库操作的人员进行了抽象。类图中没有提到接下来需要给大家展示的两个类,一个是User类,一个是Department类,因为这两个类是对数据库数据的封装,和结构并没有直接关系,所以没有显示出来,在此说明一下,以免大家引起混乱。其实,静下心来细细的看,结构还是蛮清晰的。

呵呵,下面还是老样子,给大家展示一下代码。

注意:本文所有代码均在ARC环境下编译通过。

  • User类接口
1
2
3
4
5
6
#import <Foundation/Foundation.h>

@interface User :NSObject
@property int *ID;
@property NSString *Name;
@end
  • User类实现
1
2
3
4
5
6
#import "User.h"

@implementation User
@synthesize Name =_Name;
@synthesize ID =_ID;
@end
  • Department类接口
1
2
3
4
5
6
#import <Foundation/Foundation.h> 

@interface Department:NSObject
@property int *ID;
@property NSString *DeptName;
@end
  • Department类实现
1
2
3
4
5
6
#import "Department.h"

@implementation Department
@synthesize ID =_ID;
@synthesize DeptName =_DeptName;
@end
  • IDepartment类接口
1
2
3
4
5
6
7
#import <Foundation/Foundation.h>  

@class Department;
@interface IDepartment :NSObject
-(void)Insert:(Department*)department;
-(Department*)GetDepartment:(int)myId;
@end
  • IDepartment类实现
1
2
3
4
5
6
7
8
9
10
11
#import "IDepartment.h"
#import "Department.h"

@implementation IDepartment
-(void)Insert:(Department *)department{
    return;
}
-(Department*)GetDepartment:(int)myId{
    return nil;
}
@end
  • SqlserverDepartment类接口
1
2
3
4
#import "IDepartment.h"

@interface SqlserverDepartment:IDepartment
@end
  • SqlserverDepartment类实现
1
2
3
4
5
6
7
8
9
10
11
#import "SqlserverDepartment.h"

@implementation SqlserverDepartment
-(void)Insert:(Department *)department{
    NSLog(@"在SQL Server中给Department表增加一条记录");
}
-(Department*)GetDepartment:(int)myId{
    NSLog(@"在SQL Server中根据ID得到Department表一条记录");
    return nil;
}
@end
  • AccessDepartment类接口
1
2
3
4
#import "IDepartment.h"

@interface AccessDepartment:IDepartment
@end

*AccessDepartment类实现

1
2
3
4
5
6
7
8
9
10
11
#import "AccessDepartment.h"

@implementation AccessDepartment
-(void)Insert:(Department *)department{
    NSLog(@"在Access中给Department表增加一条记录");
}
-(Department*)GetDepartment:(int)myId{
    NSLog(@"在Access中根据myId得到Department表一条记录");
    return nil;
}
@end
  • IUser类接口
1
2
3
4
5
6
7
#import <Foundation/Foundation.h>

@class User;
@interfaceIUser :NSObject
-(void)Insert:(User*)user;
-(User*)GetUser:(int)myID;
@end
  • IUser类实现
1
2
3
4
5
6
7
8
9
10
11
#import "IUser.h"
#import "User.h"

@implementation IUser
-(void)Insert:(User *)user{
    return;
}
-(User*)GetUser:(int)myID{
    return nil;
}
@end
  • SqlServerUser类接口
1
2
3
4
#import "IUser.h"

@interface SqlServerUser :IUser
@end
  • SqlServerUser类实现
1
2
3
4
5
6
7
8
9
10
11
#import "SqlServerUser.h"

@implementation SqlServerUser
-(void)Insert:(User *)user{
    NSLog(@"在SQL Server中给User表增加一条记录");
}
-(User*)GetUser:(int)myID{
    NSLog(@"在SQL Server中根据myID得到User表一条记录");
    return nil;
}
@end
  • AccessUser类接口
1
2
3
4
#import "IUser.h"

@interface AccessUser :IUser
@end
  • AccessUser类实现
1
2
3
4
5
6
7
8
9
10
11
#import "AccessUser.h"

@implementation AccessUser
-(void)Insert:(User *)user{
    NSLog(@"在Access中给User表增加一条记录");
}
-(User*)GetUser:(int)myID{
    NSLog(@"在Access中根据myID得到User表一条记录");
    return nil;
}
@end
  • IFactories类接口
1
2
3
4
5
6
7
8
9
10
11
#import "AccessUser.h"

@implementation AccessUser
-(void)Insert:(User *)user{
    NSLog(@"在Access中给User表增加一条记录");
}
-(User*)GetUser:(int)myID{
    NSLog(@"在Access中根据myID得到User表一条记录");
    return nil;
}
@end
  • IFactories类实现
1
2
3
4
5
6
7
8
9
10
11
12
#import "IFactories.h"
#import "IUser.h"
#import "IDepartment.h"

@implementation IFactories
-(IUser*)CreateUser{
    return nil;
}
-(IDepartment*)CreateDepartment{
    return nil;
}
@end
  • AccessFactory类接口
1
2
3
4
#import "IFactories.h"

@interface AccessFactory :IFactories
@end
  • AccessFactory类实现
1
2
3
4
5
6
7
8
9
10
11
12
#import "AccessFactory.h"
#import "AccessUser.h"
#import "AccessDepartment.h"

@implementation AccessFactory
-(IUser*)CreateUser{
    return [[AccessUser alloc]init];
}
-(IDepartment*)CreateDepartment{
    return [[AccessDepartment alloc]init];
}
@end
  • SqlServerFactory类接口
1
2
3
4
#import "IFactories.h"

@interface SqlServerFactory :IFactories
@end
  • SqlServerFactory类实现
1
2
3
4
5
6
7
8
9
10
11
12
#import "SqlServerFactory.h"
#import "SqlServerUser.h"
#import "SqlserverDepartment.h"

@implementation SqlServerFactory
-(IUser*)CreateUser{
    return [[SqlServerUser alloc]init];
}
-(IDepartment*)CreateDepartment{
    return [[SqlserverDepartment alloc]init];
}
@end
  • Main方法调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#import <Foundation/Foundation.h>
#import "User.h"
#import "Department.h"
#import "IFactories.h"
#import "AccessFactory.h"
#import "IUser.h"
#import "IDepartment.h"

int main (int argc,const char * argv[])
{
    @autoreleasepool{
        User *user = [[User alloc]init];
        Department *dept = [[Department alloc]init];
        IFactories *factories = [[AccessFactory alloc]init];
        IUser *iu = [factories CreateUser];
        [iu Insert:user];
        [iu GetUser:1];

        IDepartment *myId = [factories CreateDepartment];
        [myId Insert:dept];
        [myId GetDepartment:1];
    }
    return 0;
}

上面罗列了一堆代码,其实,罗列这些代码的目的只有一个,就是为了帮助像我一样基础不太好的同学尽快入门,有一个感性的认识,迈过第一道门槛。

好啦,闲话先不多说,下面给出原汁原味的类结构图,大家可以类比上面的图进行理解。

pic

仔细看上面这张图,是不是和开始举的那个例子的类结构图很像呢!?对应关系简单罗列一下:

原味图—————————–例子图
AbstractFactory————–IFactory
ConcreteFactory1———–SqlServerFactory
ConcreteFactory2———–AccessFactory
AbstractProductA———–IUser
AbstractProductB———–IDepartment

好啦,仔细看看,很像吧,呵呵!今天东西看起来不算少,关键还在于融会贯通呀!

Comments