1 #include <iostream> 2 3 enum CTYPE {PRODUCTA,PRODUCTB}; 4 class Product{ 5 public: 6 virtual void func() = 0; 7 virtual ~Product(){}; 8 }; 9 10 class ProductA : public Product{ 11 public: 12 void func(){ 13 std::cout << "create ProductA..." << std::endl; 14 } 15 }; 16 17 class ProductB : public Product{ 18 public: 19 void func(){ 20 std::cout << "create ProductB..." << std::endl; 21 } 22 }; 23 24 class Factory{ 25 public: 26 static Product* createProduct(enum CTYPE ctype){ 27 if(ctype == PRODUCTA) 28 return new ProductA; 29 else if(ctype == PRODUCTB){ 30 return new ProductB; 31 }else 32 return nullptr; 33 } 34 }; 35 36 int main(){ 37 Product* p = Factory::createProduct(CTYPE::PRODUCTA); 38 p->func(); 39 return 0; 40 }