The most basic factory model in Design Patterns
It is determined by the type of input and the type of operation to be selected.
The idea is basically the same as that of input 1 in the process orientation to execute func1 (); input 2 to execute func2 ().
# Include <iostream> using namespace STD; Enum eshoetype {leather = 0, rubber}; Class cshoe {public: Virtual void what () = 0 ;}; class cleathershoe: public cshoe {public: void what () {cout <"I Am Leather shoe. "<Endl ;}}; class crubbershoe: Public cshoe {public: void what () {cout <" I am rubber shoe. "<Endl ;}}; class cshoefactory {public: cshoe * getshowinstance (eshoetype type) {cout <" factory showinstance... "<Endl; Switch (type) {Case Leather: return New cleathershoe (); Case rubber: return New crubbershoe (); default: return NULL ;}}; void testwithcpp () {cshoefactory factory; cshoe * pshoe = NULL; pshoe = factory. getshowinstance (leather); If (null! = Pshoe) {pshoe-> what (); Delete pshoe; pshoe = NULL;} pshoe = factory. getshowinstance (rubber); If (null! = Pshoe) {pshoe-> what (); Delete pshoe; pshoe = NULL ;}} //////////////////////////////////////// ////////////// typedef struct shoe {int type; void (* print_shoe) (struct shoe *);} shoe; void printleathershoe (shoe * pshoe) {If (null! = Pshoe) printf ("I Am Leather shoe \ n");} void printrubbershoe (shoe * pshoe) {If (null! = Pshoe) printf ("I am rubber shoe \ n");} shoe * factoryshoe (INT type) {shoe * pshoe = NULL; pshoe = (shoe *) malloc (sizeof (shoe); If (null = pshoe) return NULL; memset (pshoe, 0, sizeof (shoe); If (leather = type) {pshoe-> type = type; pshoe-> print_shoe = printleathershoe;} else if (rubber = type) {pshoe-> type = type; pshoe-> print_shoe = printrubbershoe ;} else {free (pshoe); pshoe = NULL; return NULL;} return pshoe;} void t Estwithc () {printf ("\ n test C version factory mode \ n"); shoe * pshoe = factoryshoe (leather); If (null! = Pshoe) {pshoe-> print_shoe (pshoe); free (pshoe); pshoe = NULL;} pshoe = factoryshoe (rubber); If (null! = Pshoe) {pshoe-> print_shoe (pshoe); free (pshoe); pshoe = NULL;} pshoe = factoryshoe (994); If (null! = Pshoe) {pshoe-> print_shoe (pshoe); free (pshoe); pshoe = NULL ;}} //////////////////////////////////////// //// // int _ tmain (INT argc, _ tchar * argv []) {testwithcpp (); testwithc (); Return 0 ;}