Abstract Factory (Abstract Factory mode): Provides an interface that creates a series of related or interdependent objects without specifying their specific classes.Abstract Factory mode (Factory)Chase mm, please eat, McDonald's chicken wings and KFC chicken wings are all mm love to eat things, although the taste is different, but whether you take mm to McDonald's or KFC, just to the waiter said "to four chicken wings" on the line. McDonald's and KFC are the factory to produce chicken wings.Factory mode: Separate the customer class from the factory class. Consumers need a product at any time, just request it from the factory. Consumers can accept new products without modification. The disadvantage is that when the product is modified,Factory classAlso make the corresponding changes. such as: How to create and how to provide to the client.
User abstract interface class iuser{public:virtual void GetUser () = 0;virtual void Insertuser () = 0;};/ /Department Abstract interface Class idepartment{public:virtual void getdepartment () = 0;virtual void insertdepartment () = 0;};/ /acess User class caccessuser:public iuser{public:virtual void GetUser () {cout << "Access GetUser" << Endl;} virtual void Insertuser () {cout << Access insertuser << endl;}};/ /access Department class caccessdepartment:public idepartment{public:virtual void Getdepartment () {cout << Access Getdepartment "<< Endl;} virtual void Insertdepartment () {cout << Access insertdepartment << endl;}};/ /sql User class csqluser:public iuser{public:virtual void GetUser () {cout << "Sql user" << Endl;} virtual void Insertuser () {cout << Sql Insert User << endl;}};/ /sql Department class csqldepartment:public idepartment{public:virtual void Getdepartment () {cout << "Sql Department" <& Lt Endl;} virtual void Insertdepartment () {cout << Sql Insert Department << endl;}};/ /abstract Factory class ifactory{public:virtual Iuser * CreateUser () = 0;virtual idepartment * createdepartment () = 0;};/ /access Factory class Accessfactory:public ifactory{public:virtual Iuser * CreateUser () {return new Caccessuser (); Virtual Idepartment * Createdepartment () {return new caccessdepartment ();}};/ /sql Factory class Sqlfactory:public ifactory{public:virtual Iuser * CreateUser () {return new Csqluser (); Virtual Idepartment * Createdepartment () {return new csqldepartment ();}}; int _tmain (int argc, _TCHAR * argv[]) {Ifactory * factory = new sqlfactory (); Iuser * user = Factory->createuser (); Idepar Tment * depart = Factory->createdepartment (); User->getuser ();d epart->getdepartment (); return 0;}
The key point is
C + + design mode from 0 to abstract Factory mode