Today, I saw chapter 8th of "big talk Design Patterns" and learned the principles of factory method patterns.
In general, the concept of the factory model is centered around: open for extension, close for modification is a product of the design principle of "open-closed principle.
I don't know what it means because I have little experience.
At last, I will summarize my doubts.
The exercise code is described here.
[Computing base class]
class Operator{public: Operator(void); virtual ~Operator(void); virtual double GetResult(void) const = 0; public: double _numberFirst = 0; double _numberSecond = 0;};Operator::Operator(void){ }Operator::~Operator(void){ }
[Addition operation class]
/* Addition operation class */class operatoradd: public operator {public: operatoradd (void); Virtual ~ Operatoradd (void); Virtual double getresult (void) const ;}; operatoradd: operatoradd (void) {STD: cout <"addition operation class constructor" <STD:: Endl;} operatoradd ::~ Operatoradd (void) {} double operatoradd: getresult (void) const {STD: cout <"Call the getresult method of the addition class" <STD: Endl; double result = _ numberfirst + _ numbersecond; return result ;}
Subtraction operation class]
/* Subtraction operation class */class operatorsub: public operator {public: operatorsub (void); Virtual ~ Operatorsub (void); Virtual double getresult (void) const ;}; operatorsub: operatorsub (void) {STD: cout <"subtraction constructor" <STD:: Endl;} operatorsub ::~ Operatorsub (void) {} double operatorsub: getresult (void) const {STD: cout <"Call the getresult method of the subtraction class" <STD: Endl; double result = _ numberfirst-_ numbersecond; return result ;}
[Factory base class]
// Factory base class factory {public: Factory (void); Virtual ~ Factory (void); virtual operator * createoperation (void) = 0 ;}; Factory: Factory (void) {} factory ::~ Factory (void ){}
[Addition factory]
// Addition factory class operatoraddfactory: Public factory {public: operatoraddfactory (void); Virtual ~ Operatoraddfactory (void); virtual operator * createoperation (void) ;}; operatoraddfactory: operatoraddfactory (void) {} operatoraddfactory ::~ Operatoraddfactory (void) {} operator * operatoraddfactory: createoperation (void) {return New operatoradd ();}
Subtraction Factory]
// Subtraction factory class operatorsubfactory: Public factory {public: operatorsubfactory (void); Virtual ~ Operatorsubfactory (void); virtual operator * createoperation (void) ;}; operatorsubfactory: operatorsubfactory (void) {} operatorsubfactory ::~ Operatorsubfactory (void) {} operator * operatorsubfactory: createoperation (void) {return New operatorsub ();}
[Main]
int main(int argc, const char * argv[]){ Factory* factory = new OperatorAddFactory(); Operator* oper = factory->CreateOperation(); oper->_numberFirst = 1; oper->_numberSecond = 4; std::cout << oper->GetResult() << std::endl; delete oper; oper = NULL; delete factory; factory = NULL; return 0;}
Result]
Addition operation Constructor
Call the addition classGetresultMethod
5
[Doubt]
The main function here is changed to the following:
int main(int argc, const char * argv[]){// Factory* factory = new OperatorSubFactory();// // Operator* oper = factory->CreateOperation(); Operator* oper = new OperatorAdd(); oper->_numberFirst = 1; oper->_numberSecond = 4; std::cout << oper->GetResult() << std::endl; delete oper; oper = NULL; // delete factory;// factory = NULL; return 0;}
The results are the same. Where are the advantages of the factory method?
What are the specific application features?
-- 2013/08/20 mark