0%
- 动机
- 在软件系统中,经常面临着创建对象的工作, “一系列相互依赖的对象” 的创建工作;同时,由于需求的变化,往往存在更多系列的创建工作
例子
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
| class IDBConnection{
}; class IDBConnectiornFactory{ public: virtual IDBConnection* CreateDBConnection()=0; } class IDBCommand{
} class IDBCommandFactory{ virtual IDBCommand* CreateIDBCommand()=0; } class IDataReader{
} class IDataReaderFactory{ virtual IDataReader* CreateIDataReader()=0; }
class SqlConnection:public IDBConnection{
}
class SqlConnectionFactory:public IDBConnectiornFactory{
} class SqlCommand:public IDBCommand{
}
class SqlCommandFatory:public IDBCommandFactory{
} class SqlReader:public IDataReader{
}
class SqlReaderFatory:public IDataReaderFactory{ }
class OracalConnection:public IDBConnection{
} class OracalConnectionFactory:public IDBConnectiornFactory{
}
class OracalCommand:public IDBCommand{
} class OracalCommandFatory:public IDBCommandFactory{
} class OracalReader:public IDataReader{
} class OracalReaderFatory:public IDataReaderFactory{ }
class EmployeeDAO{ IDBConnectiornFatory* dbConnectiornFatory; IDBCommandFactory* dbCommandFatory; IDataReaderFactory* dbDataReaderFactory; public: vector<EmployeeDo> GetEmployees(){ IDBConnection* connection = IDBConnectiornFatory->createDBConnection(); connection->ConnectionString("...."); IDBCommand* command = IDBCommandFactory->createDBCommand(); command->SetConnection(connection); command->CommandText("...."); IDataReader* reader =command->ExecuteReader(); while(reader->Read()){
} } }
|
问题
- 由于不同的工厂使用中需要进行搭配,有相关性的
- 既然有强相关性,要放在一起
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
| class IDBConnection{
}; class IDBCommand{
};
class IDataReader{
};
class IDBFactory{ public: virtual IDBConnection* CreateDBConnection()=0; virtual IDBCommand* CreateIDBCommand()=0; virtual IDataReader* CreateIDataReader()=0; }
class SqlConnection:public IDBConnection{
}
class SqlCommand:public IDBCommand{
}
class SqlReader:public IDataReader{
}
class SqlDBFactory:public IDBFactory{
}
class OracalConnection:public IDBConnection{
}
class OracalCommand:public IDBCommand{
}
class OracalReader:public IDataReader{
}
class OracalDBFactory:public IDBFactory{
}
class EmployeeDAO{ IDBFactory* dbFactory;
public: vector<EmployeeDo> GetEmployees(){ IDBConnection* connection = dbFactory->createDBConnection(); connection->ConnectionString("...."); IDBCommand* command = dbFactory->createDBCommand(); command->SetConnection(connection); command->CommandText("...."); IDataReader* reader =command->ExecuteReader(); while(reader->Read()){
} } }
|
UML图
要点总结
- 如果没有对应 “多系列对象创建” 的需求变化,则没有必要使用AbstractFactory模式,这时候使用简单的工厂完全可以
- “系列对象” 指的是在某一特定系列下的对象之间的相互依赖,或作用的关系。不同系列的对象之间不能相互依赖
- 缺点
- 在于对应 “新系列” 的需求变动,其缺点在于难以应对 “新对象” 的需求变动