Chapter 1 of "big talk Design Pattern" is the best code if there is no error? -- Summary of simple factory Model

Source: Internet
Author: User
Tags class operator

Use the object-oriented programming language to implement a console program. You must enter two numbers and operator numbers to obtain the result"

Introduce [Object-Oriented Programming] and point out three important features

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
Pointed out the programming requirements:
  1. Maintenance
  2. Reusable
  3. Scalable
Here we will summarize the design ideas: according to the requirements of the questions, we can divide them into the following steps,
  • Enter two numbers
  • Input operator number
  • Calculation Result
  • Output result
Here we will refer to the [logical part] and [display part]. In the [logical part], we want to establish a class for computing. Why should this class be the [Operational class] operation? -- If you want to add an operation called "fight", that is, "x fight y", consider the object-oriented scalability, we should simply add a class related to fight to fulfill this expansion requirement, while fight is an operation. Therefore, it is obviously necessary to add a fight operation class. As a result, in addition to the fight operation class, there are still many operation classes. Therefore, you can design an operation base class. These specific operation classes are designed as subclasses of the operation base class. To obtain the specific base Class and Class: [base class]
class Operator{public:Operator(void);virtual ~Operator(void);virtual double GetResult() 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 () const ;}; operatoradd: operatoradd (void) {STD: cout <"addition operation class constructor" <STD :: endl;} operatoradd ::~ Operatoradd (void) {} double operatoradd: getresult () 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 () const ;}; operatorsub: operatorsub (void) {STD: cout <"subtraction operation class constructor" <STD :: endl;} operatorsub ::~ Operatorsub (void) {} double operatorsub: getresult () const {STD: cout <"Call the getresult method of the subtraction class" <STD: Endl; double result = _ numberfirst-_ numbersecond; return result ;}

Simple factory class: returns the corresponding operation class for the input.

/* Factory class */# include <cassert> class operatorfactory {public: operatorfactory (void );~ Operatorfactory (void); Public:/* Static factory Method */static operator * createoperator (const STD: string & Model) ;}; operatorfactory: operatorfactory (void) {} operatorfactory ::~ Operatorfactory (void) {} operator * operatorfactory: createoperator (const STD: string & Model) {If (model = "+") {return New operatoradd ();} else if (model = "-") {return New operatorsub () ;}else {assert (false) ;}return null ;}
[Main]
int main(int argc, const char * argv[]){Operator * ope = NULL;std::string modeName = "-";ope = OperatorFactory::CreateOperator(modeName);    ope->_numberFirst = 4;    ope->_numberSecond = 5;cout << ope->GetResult();    delete ope;ope = NULL;return 0;}

[Output result]

Subtraction Constructor

Call the subtraction classGetresultMethod

-1

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.