5.2 Abstract Factory mode
motivation: in software systems, the creation of "a series of interdependent objects" is often faced, and as the requirements change, more and more series objects are created.
code example:
Realize the use of database business logic, support multi-database (sql,oracle, etc.), have connection, command, read and other functions.
where commands, connection functions are interconnected.
Method one (using the factory method):
Each feature class has a factory created, such as IDbConnection and Idbconnectionfactory
1//Database access related base class 2 class idbconnection{3 4}; 5 class idbconnectionfactory{6 public:7 virtual idbconnection* createdbconnection () = 0; 8}; 9 class idbcommand{12};14 class idbcommandfactory{15 public:16 virtual idbcommand* createdbcommand () =0;1 7};18 class idatareader{21};23 class idatareaderfactory{24 public:25 virtual idatareader* Createdatarea Der () =0;26};27 28 29//Support SQL Server30 class Sqlconnection:public idbconnection{31};33 class Sqlconnectionfactory :p ublic idbconnectionfactory{34};36 PNS class Sqlcommand:public idbcommand{39-};41 class Sqlcommandfacto Ry:public idbcommandfactory{42};44 class Sqldatareader:public idatareader{47-};49 class Sqldatareade Rfactory:public idatareaderfactory{50 51};52 53//Support ORACLE54 class Oracleconnection:public idbconnection{55 56} , Oraclecommand:public idbcommand{59};61 class Oracledatareader:public idatareader{63 64};employeedao{69 idbconnectionfactory* dbconnectionfactory;70 idbcommandfactory* dbCommandFactory; idatareaderfactory* datareaderfactory;72 public:75 vector<employeedo> getemployees () {76 idbconnection* connection =77 dbconnectionfactory->createdbconnection (...); Connection->connectionstring ("..."); idbcommand* command =81 dbcommandfactory->c Reatedbcommand (...); Command->commandtext ("..."); Command->setconnection (connection); Relevance of idbdatareader* reader = Command->executereader (); Correlation (Reader->read ()) {87 88}89 90}91};
Parsing the above code, while resolving the problem of component creation. But careful consideration, because of the correlation between functions, the objects of different types of database can not be created together (such as SQL command and Oracle's connection collocation, obviously unreasonable). So consider the abstract factory model. See method Two:
Using a factory, a series of interdependent objects are created in a factory.
1//Database access related base class 2 class idbconnection{3 4}; 5 6 class idbcommand{7 8}; 9 class idatareader{11};13 class idbfactory{16 public:17 virtual idbconnection* createdbconnection () = 0;18 virtual idbcommand* createdbcommand () =0;19 virtual idatareader* createdatareader () =0;20 21};22 23 24//Branch Holding SQL Server25 class Sqlconnection:public idbconnection{26};28 class Sqlcommand:public idbcommand{29 30};31 Class Sqldatareader:public idatareader{32};34 class sqldbfactory:public idbfactory{37 public:38 virtual idbconnection* createdbconnection () =0;39 virtual idbcommand* createdbcommand () =0;40 virtual idatareader* CreateDa Tareader () =0;41 42};43 44//Support ORACLE45 class Oracleconnection:public idbconnection{46-};48-Class Oraclecomman D:public idbcommand{50-};52-Class Oracledatareader:public idatareader{54-};56-A-class Employeeda o{60 idbfactory* dbfactory;61 public:63 vectoR<employeedo> GetEmployees () {idbconnection* connection =65 dbfactory->createdbconnection () ; Connection->connectionstring ("..."); idbcommand* command =69 dbfactory->created Bcommand (); Command->commandtext ("..."); Command->setconnection (connection); Relevance of the idbdatareader* reader = Command->executereader (); Correlation (Reader->read ()) {75 76}77 78}79};
Schema Definition:
Provides an interface to complicate the creation of a series of "related or interdependent objects" without specifying their specific classes.
Class Diagram:
Summary of key points:
If there is no demand change for multi-series object building, it is not necessary to use the abstract Factory mode, which is to use the factory method.
A "series object" refers to a relationship between objects in a particular series that is interdependent or useful. Objects of different series cannot depend on each other.
The Abstract factory model mainly lies in responding to the "new series" of demand changes. The disadvantage is that it is difficult to cope with changes in demand for "new objects".
C + + Design mode 9 (abstract Factory abstraction Factory mode)