Big talk design model-simple factory Model

Source: Internet
Author: User

It is about simple binary mathematical operations: addition, subtraction, multiplication, division, and so on.

The book is described in the non-C ++ language, but I still like C ++ a little more. The C ++ version is changed below.

Operation Base class:

Class operation:

{

Public:

Operation (): m_numa (0), m_numb (0 ){}

Virtual double getresult () {return 0 ;}

Void setoperationnum (double NUMA, double numb)

{

M_numa = NUMA;

M_numb = numb;

}

PRIVATE:

Double m_numa;

Double m_numb;

};

Addition class:

Class operationadd: Public operation

{

Public:

Virtual double getresult ()

{

Return m_numa + m_numb;

}

}

Similar to operationsub multiplication operationmul

Then implement the computing factory class:

Class operationfactory:

{

Public:

Operation * createoperate (const string & operate)

{

Operation * operator;

Switch (operate)

{

Case "+": condition = new operationadd (); break;

Case "-": condition = new operationsub (); break;

......

}

}

}

Practical application:

Operation * operator;

Operationfactory factory;

Operator = factory. createoperate ("+ ");

Handler-> setoperationnum (2, 3 );

Double result = response-> getresult ();

Delete objects;

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.