概述
- 属于“对象创建”模式,解决了“多系列对象创建”的需求变化
- EmployeeDAO1不支持多种数据库,需采用面向接口的对象编程
- EmployeeDAO2用了工厂模式,但未完全解决问题。command和connection必须是同一类型,存在安全隐患
- EmployeeDAO3采用抽象工厂模式,将相互依赖的对象放在一个工厂类中,解决了相互依赖对象的创建工作
- 提供一个接口,让该接口负责创建一系列“相关或者相互依赖的对象”,而无需指定具体类
- 红色--稳定,蓝色--第一种变化(SQL),绿色--第二种变化(Oracle)
- 每个产品由多个部分构成,每个部分由专门的工厂生产
- 如果没有应对“多系列对象创建”的需求变化,则没有必要使用Abstract Factory模式,使用简单工厂即可
- “系列对象”指某特定系列下对象间有相互依赖或作用的关系,不同系列对象间不能相互依赖
- Abstract Factory模式主要在于应对“新系列”的需求变动,缺点在于难以应对“新对象”的需求变动
- 稳定的地方就是缺点,稳定的地方变了,就要改用其他设计模式
- 任何地方都可能变化,则没有模式或其组合能解决问题;任何地方都稳定,则没有用模式的必要
示例1
EmpoyeeDAO01
1 class EmployeeDAO{ 2 3 public: 4 vector<EmployeeDO> GetEmployees(){ 5 SqlConnection* connection = 6 new SqlConnection(); 7 connection->ConnectionString = "..."; 8 9 SqlCommand* command = 10 new SqlCommand(); 11 command->CommandText="..."; 12 command->SetConnection(connection); 13 14 SqlDataReader* reader = command->ExecuteReader(); 15 while (reader->Read()){ 16 17 } 18 19 } 20 };
EmpoyeeDAO02
1 //数据库访问有关的基类 2 class IDBConnection{ 3 4 }; 5 class IDBConnectionFactory{ 6 public: 7 virtual IDBConnection* CreateDBConnection()=0; 8 }; 9 10 class IDBCommand{ 11 12 }; 13 class IDBCommandFactory{ 14 public: 15 virtual IDBCommand* CreateDBCommand()=0; 16 }; 17 18 class IDataReader{ 19 20 }; 21 class IDataReaderFactory{ 22 public: 23 virtual IDataReader* CreateDataReader()=0; 24 }; 25 26 //支持SQL Server 27 class SqlConnection: public IDBConnection{ 28 29 }; 30 class SqlConnectionFactory:public IDBConnectionFactory{ 31 32 }; 33 34 class SqlCommand: public IDBCommand{ 35 36 }; 37 class SqlCommandFactory:public IDBCommandFactory{ 38 39 }; 40 41 class SqlDataReader: public IDataReader{ 42 43 }; 44 class SqlDataReaderFactory:public IDataReaderFactory{ 45 46 }; 47 48 //支持Oracle 49 class OracleConnection: public IDBConnection{ 50 51 }; 52 //... 53 class OracleCommand: public IDBCommand{ 54 55 }; 56 //... 57 class OracleDataReader: public IDataReader{ 58 59 }; 60 //... 61 62 class EmployeeDAO{ 63 IDBConnectionFactory* dbConnectionFactory; 64 IDBCommandFactory* dbCommandFactory; 65 IDataReaderFactory* dataReaderFactory; 66 67 public: 68 vector<EmployeeDO> GetEmployees(){ 69 IDBConnection* connection = 70 dbConnectionFactory->CreateDBConnection(); 71 connection->ConnectionString("..."); 72 73 IDBCommand* command = 74 dbCommandFactory->CreateDBCommand(); 75 command->CommandText("..."); 76 command->SetConnection(connection); //关联性 77 78 IDBDataReader* reader = command->ExecuteReader(); //关联性 79 while (reader->Read()){ 80 81 } 82 } 83 };
EmpoyeeDAO03
1 //数据库访问有关的基类 2 class IDBConnection{ 3 4 }; 5 6 class IDBCommand{ 7 8 }; 9 10 class IDataReader{ 11 12 }; 13 14 class IDBFactory{ 15 public: 16 virtual IDBConnection* CreateDBConnection()=0; 17 virtual IDBCommand* CreateDBCommand()=0; 18 virtual IDataReader* CreateDataReader()=0; 19 20 }; 21 22 //支持SQL Server 23 class SqlConnection: public IDBConnection{ 24 25 }; 26 class SqlCommand: public IDBCommand{ 27 28 }; 29 class SqlDataReader: public IDataReader{ 30 31 }; 32 33 class SqlDBFactory:public IDBFactory{ 34 public: 35 virtual IDBConnection* CreateDBConnection()=0; 36 virtual IDBCommand* CreateDBCommand()=0; 37 virtual IDataReader* CreateDataReader()=0; 38 39 }; 40 41 //支持Oracle 42 class OracleConnection: public IDBConnection{ 43 44 }; 45 46 class OracleCommand: public IDBCommand{ 47 48 }; 49 50 class OracleDataReader: public IDataReader{ 51 52 }; 53 54 class EmployeeDAO{ 55 IDBFactory* dbFactory; 56 57 public: 58 vector<EmployeeDO> GetEmployees(){ 59 IDBConnection* connection = 60 dbFactory->CreateDBConnection(); 61 connection->ConnectionString("..."); 62 63 IDBCommand* command = 64 dbFactory->CreateDBCommand(); 65 command->CommandText("..."); 66 command->SetConnection(connection); //关联性 67 68 IDBDataReader* reader = command->ExecuteReader(); //关联性 69 while (reader->Read()){ 70 71 } 72 73 } 74 };