设计模式-抽象工厂

1 抽象工厂模式

  • 在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作;同时,由于需求的变化,往往存在更多系列对象的创建工作。
  • 如何应对这种变化?如何绕过常规的对象创建方法(new),提供一种“封装机制”来避免客户程序和这种“多系列具体对象创建工作”的紧耦合?
  • 提供一个接口,让该接口负责创建一系列“相关或者相互依赖的对象”,无需指定它们具体的类。

2 问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class EmployeeDAO{
public:
vector<EmployeeDO> GetEmployees(){
SqlConnection* connection = new SqlConnection(); // 耦合
connection->ConnectionString("...");

SqlCommand* command = new SqlCommand(); // 耦合
command->CommandText("...");
command->SetConnection(connection);

SqlDataReader* reader = command->ExecuteReader();
while (reader->Read()){
// ...
}
}
};

3 尝试简单工厂模式

系列对象的关联性丢失,产生大量类,代码臃肿不堪

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 先尝试用简单工厂模式解决...
// 数据库访问有关的基类
class IDBConnection{
// ...
};
class IDBConnectionFactory{
public:
virtual IDBConnection* CreateDBConnection()=0;
};


class IDBCommand{
// ...
};
class IDBCommandFactory{
public:
virtual IDBCommand* CreateDBCommand()=0;
};


class IDBDataReader{
// ...
};
class IDBDataReaderFactory{
public:
virtual IDBDataReader* CreateDataReader()=0;
};



// 支持SQL Server
class SqlConnection: public IDBConnection{
// ...
};
class SqlConnectionFactory:public IDBConnectionFactory{
// ...
};

class SqlCommand: public IDBCommand{
// ...
};
class SqlCommandFactory:public IDBCommandFactory{
// ...
};

class SqlDataReader: public IDataReader{
// ...
};
class SqlDataReaderFactory:public IDBDataReaderFactory{
// ...
};


// 支持Oracle
class OracleConnection: public IDBConnection{
// ...
};
class OracleConnectionFactory: public IDBConnectionFactory{
// ...
}

class OracleCommand: public IDBCommand{
// ...
};
class OracleCommandFactory: publc IDBCommandFactory{
// ...
}

class OracleDataReader: public IDataReader{
// ...
};
class OracleDataReaderFactory: public IDBDataReaderFactory{
// ...
}

class EmployeeDAO{
IDBConnectionFactory* dbConnectionFactory;
IDBCommandFactory* dbCommandFactory;
IDBDataReaderFactory* dataReaderFactory;

public:
vector<EmployeeDO> GetEmployees(){
IDBConnection* connection = dbConnectionFactory->CreateDBConnection();
connection->ConnectionString("...");

IDBCommand* command = dbCommandFactory->CreateDBCommand();
command->CommandText("...");
command->SetConnection(connection); //关联性

IDBDataReader* reader = command->ExecuteReader(); //关联性
while (reader->Read()){
// ...
}
}
};

4 抽象工厂(family factory)

抽象工厂模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// family factory
// 数据库访问有关的基类
class IDBConnection{

};
class IDBCommand{

};
class IDBDataReader{

};

// 抽象工厂基类
class IDBFactory{
public:
virtual IDBConnection* CreateDBConnection()=0;
virtual IDBCommand* CreateDBCommand()=0;
virtual IDBDataReader* CreateDataReader()=0;

};


// 支持SQL Server
class SqlConnection: public IDBConnection{

};
class SqlCommand: public IDBCommand{

};
class SqlDataReader: public IDBDataReader{

};
// SQL工厂
class SqlDBFactory:public IDBFactory{
public:
virtual IDBConnection* CreateDBConnection(){
return new SqlConnection();
}
virtual IDBCommand* CreateDBCommand(){
return new SqlCommand();
}
virtual IDBDataReader* CreateDataReader(){
return new SqlDataReader();
}
};


// 支持Oracle
class OracleConnection: public IDBConnection{

};
class OracleCommand: public IDBCommand{

};
class OracleDataReader: public IDBDataReader{

};
// Oracle工厂
class OracleDBFactory: public IDBFactory{
public:
virtual IDBConnection* CreateDBConnection(){
return new OracleConnection();
}
virtual IDBCommand* CreateDBCommand(){
return new OracleCommand();
}
virtual IDBDataReader* CreateDataReader(){
return new OracleDataReader();
}
}


class EmployeeDAO{
IDBFactory* dbFactory; // 抽象工厂指针

public:
vector<EmployeeDO> GetEmployees(){
IDBConnection* connection = dbFactory->CreateDBConnection();
connection->ConnectionString("...");

IDBCommand* command = dbFactory->CreateDBCommand();
command->CommandText("...");
command->SetConnection(connection); //关联性

IDBDataReader* reader = command->ExecuteReader(); //关联性
while (reader->Read()){
// ...
}

}
};

5 总结

  • 如果没有应对“多系列对象构建”的需求变化,则没有必要使用Abstract Factory模式,这时候使用简单的工厂完全可以。
  • “系列对象”指的是在某一特定系列下的对象之间有相互依赖、或作用的关系。不同系列的对象之间不能相互依赖。
  • Abstract Factory模式主要在于应对“新系列”的需求变动。其缺点在于难以应对“新对象”的需求变动。(新系列数据库:OK;新方法IDBMethod:NO!)

6 参考