Chapter 1 of "big talk Design Model" Lei Feng is still on Earth-Summary of the factory method model

Source: Internet
Author: User
Tags class operator

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

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.