Design Pattern factory pattern exercises

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.