c++设计模式之抽象工厂

视频地址:

https://www.ixigua.com/6804249883344634380?id=6803643457479901700

整理文档地址:

http://t.csdn.cn/IQLaf

样例类图如下:

 

 

样例代码:

  1 #include<iostream>
  2 class IDBConnection {
  3 public:
  4     virtual void ConnectionString() = 0;
  5     virtual ~IDBConnection() {}
  6 };
  7 
  8 class IDBCommand {
  9 public:
 10     virtual void SetConnection() = 0;
 11     virtual ~IDBCommand() {}
 12 };
 13 
 14 class IDataReader {
 15 public:
 16     virtual void Read() = 0;
 17     virtual ~IDataReader() {}
 18 };
 19 
 20 //支持SQL Server
 21 class SqlConnection : public IDBConnection {
 22 public:
 23     virtual void ConnectionString()
 24     {
 25         std::cout << "SqlConnection" << std::endl;
 26     }
 27 };
 28 class SqlCommand : public IDBCommand {
 29 public:
 30     virtual void SetConnection()
 31     {
 32         std::cout << "SqlCommand" << std::endl;
 33     }
 34 };
 35 class SqlDataReader : public IDataReader {
 36 public:
 37     virtual void Read()
 38     {
 39         std::cout << "SqlDataReader" << std::endl;
 40     }
 41 };
 42 
 43 //支持Oracle
 44 class OracleConnection : public IDBConnection {
 45 public:
 46     virtual void ConnectionString()
 47     {
 48         std::cout << "OracleConnection" << std::endl;
 49     }
 50 };
 51 
 52 class OracleCommand : public IDBCommand {
 53 public:
 54     virtual void SetConnection()
 55     {
 56         std::cout << "OracleCommand" << std::endl;
 57     }
 58 };
 59 
 60 class OracleDataReader : public IDataReader {
 61 public:
 62     virtual void Read()
 63     {
 64         std::cout << "OracleDataReader" << std::endl;
 65     }
 66 };
 67 
 68 class IDBFactory {
 69 public:
 70     virtual IDBConnection* CreateDBConnection() = 0;
 71     virtual IDBCommand* CreateDBCommand() = 0;
 72     virtual IDataReader* CreateDataReader() = 0;
 73 
 74 };
 75 
 76 class SqlDBFactory :public IDBFactory
 77 {
 78 public:
 79     virtual IDBConnection* CreateDBConnection()
 80     {
 81         return new SqlConnection();
 82     }
 83     virtual IDBCommand* CreateDBCommand()
 84     {
 85         return new SqlCommand();
 86     }
 87     virtual IDataReader* CreateDataReader()
 88     {
 89         return new SqlDataReader();
 90     }
 91 };
 92 
 93 class OracleDBFactory :public IDBFactory
 94 {
 95 public:
 96     virtual IDBConnection* CreateDBConnection()
 97     {
 98         return new OracleConnection();
 99     }
100     virtual IDBCommand* CreateDBCommand()
101     {
102         return new OracleCommand();
103     }
104     virtual IDataReader* CreateDataReader()
105     {
106         return new OracleDataReader();
107     }
108 };
109 
110 class EmployeeDAO {
111 private:
112     IDBFactory* dbFactory;
113 
114 public:
115     EmployeeDAO(IDBFactory* dbfactory)
116     {
117         this->dbFactory = dbfactory;
118     }
119 
120     void GetEmployees()
121     {
122         IDBConnection* connection = dbFactory->CreateDBConnection();
123         connection->ConnectionString(/*"..."*/);
124 
125         IDBCommand* command = dbFactory->CreateDBCommand();
126         command->SetConnection(/*"connection"*/); //关联性
127 
128         IDataReader* reader = dbFactory->CreateDataReader();
129         reader->Read();
130     }
131 };
132 
133 int main()
134 {
135     IDBFactory* factoryA = new SqlDBFactory();
136     EmployeeDAO* dbA = new EmployeeDAO(factoryA);
137     dbA->GetEmployees();
138 
139     IDBFactory* factoryB = new OracleDBFactory();
140     EmployeeDAO* dbB = new EmployeeDAO(factoryB);
141     dbB->GetEmployees();
142     
143     return 0;
144 }

 

模板类图如下:

 

 

posted @ 2022-05-19 15:29  跳动的休止符  阅读(31)  评论(0)    收藏  举报