Big talk design model learning notes-simple factory model (c ++ description)

Source: Internet
Author: User

Today I learned the big talk design model. The first chapter of the book describes the simple factory model, which is described in C, so after learning, I wrote the content in the book in the form of C ++ to deepen my learning impression.


First, what is the "simple factory model" should be: Use a separate class to create an instance. Big talk design model P10)


The example used in the book is a small calculator, which should have multiple operators. After waiting for the input operands and operation operators, the input symbols are used to generate the operations for calculation. This is the application of the "simple factory mode.


The following code is described in C ++:

For ease, I omit the declaration of the U-turn file and namespace.

// Operator's abstract class Operation {public: double numberA; // two operands: double numberB; virtual double GetResult () const = 0; // Operation used for calculation };


The following are the operation definitions of addition "+", subtraction "-", multiplication "*", and division "/".

// Add class OperationAdd: public Operation {public: double GetResult () const {return numberA + numberB ;}; // subtract class OperationSub: public Operation {public: double GetResult () const {return numberA-numberB ;}; // multiplication class OperationMul: public Operation {public: double GetResult () const {return numberA * numberB ;}}; // division, considering that the divisor may be 0, numeric_limits <> in the standard library is used to output the maximum value of double. Class OperationDiv: public Operation {public: double GetResult () const {if (numberB = 0) return numeric_limits <double >:: max (); else return numberA/numberB ;}};


After the operators are defined, we start the factory class.

// Factory class OperationFactory {public: static Operation * createOperation (char c) {switch (c) {case '+': return new OperationAdd (); case '-': return new OperationSub (); case '*': return new OperationMul (); case '/': return new OperationDiv ();} // use switch to check which operator is input }};


Now let's test the above classes on the client.

Int main () {// For convenience, the following are not checked whether the input result is correct double numberA = 0, numberB = 0; char numeric; cout <"Enter the first operand:"; cin> numberA; cout <"Enter the operator:"; cin> operator; cout <"enter the second operand:"; cin> numberB; // use the static method of the factory class to create the Operation * operation = OperationFactory object of the Operation OPERATOR :: createOperation (operator); operation-> numberA = numberA; operation-> numberB = numberB; // obtain the result cout using the operator object method <"result: "<operation-> GetResult () <endl; return 0 ;}


Test results:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/19343MW5-0.jpg "title =" fruit .jpg "alt =" 164042178.jpg"/>


The corresponding UML class diagram is:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/19343L159-1.jpg "title =" UML.jpg "alt =" 164058371.jpg"/>


In fact, encapsulating the operator also helps us to modify it:

1. When we need to modify division or other existing algorithms, we only need to modify the corresponding operation algorithm class, without involving other operations.

2. When we need to add a new algorithm, we only need to add the corresponding new class, and add a switch to the factory class to easily modify the function.


This article is from the "World of Canada" blog, please be sure to keep this source http://xuyjun.blog.51cto.com/7470650/1303515

Related Article

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.