[1] What is the factory method model?
Define a factory interface for creating product objects, and postpone the actual creation to the subclass. The core factory category is no longer responsible for product creation. In this way, the core class becomes an abstract factory role and is only responsible for the interfaces that must be implemented by specific factory subclass, the advantage of further abstraction is that the factory method mode enables the system to introduce new products without modifying the specific factory role.
[2] sample code of factory method mode
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class Operation 6 { 7 public: 8 double numberA; 9 double numberB; 10 11 virtual double getResult() 12 { 13 return 0; 14 } 15 }; 16 17 class addOperation : public Operation 18 { 19 double getResult() 20 { 21 return numberA + numberB; 22 } 23 }; 24 25 26 class subOperation : public Operation 27 { 28 double getResult() 29 { 30 return numberA - numberB; 31 } 32 }; 33 34 class mulOperation : public Operation 35 { 36 double getResult() 37 { 38 return numberA * numberB; 39 } 40 }; 41 42 class divOperation : public Operation 43 { 44 double getResult() 45 { 46 return numberA / numberB; 47 } 48 }; 49 50 class IFactory 51 { 52 public: 53 virtual Operation *createOperation() = 0; 54 }; 55 56 class AddFactory : public IFactory 57 { 58 public: 59 static Operation *createOperation() 60 { 61 return new addOperation(); 62 } 63 }; 64 65 66 class SubFactory : public IFactory 67 { 68 public: 69 static Operation *createOperation() 70 { 71 return new subOperation(); 72 } 73 }; 74 75 class MulFactory : public IFactory 76 { 77 public: 78 static Operation *createOperation() 79 { 80 return new mulOperation(); 81 } 82 }; 83 84 class DivFactory : public IFactory 85 { 86 public: 87 static Operation *createOperation() 88 { 89 return new divOperation(); 90 } 91 }; 92 93 int main() 94 { 95 Operation *oper = MulFactory::createOperation(); 96 oper->numberA = 9; 97 oper->numberB = 99; 98 cout << oper->getResult() << endl; 99 return 0;100 }View code
Good good study, day up.
Sequential selection cycle Summary
Factory method mode