Design Mode: simple factory Mode

Source: Internet
Author: User

Write a C ++ calculator program using the object-oriented idea and simple factory model. The Code is as follows:

# Include <iostream> using namespace STD; Class operation {public: Operation (double left, double right) {LHS = left; RHS = right;} const double getleft () const {return LHS;} const double getright () const {return RHS;} void setleft (const double left) {LHS = left;} void setright (const double right) {RHS = right;} virtual double calculate () = 0; // pure virtual function protected: Double LHS; double RHS;}; Class Add: Public operation {public: add (double left, double right): Operation (left, right) {}double calculate () {return LHS + RHS ;}; class sub: public operation {public: Sub (double left, double right): Operation (left, right) {}double calculate () {return LHS-RHS ;}; class Mul: public operation {public: MUL (double left, double right): Operation (left, right) {} double calculate () {return LHS * RHS ;}}; class Div: Public operation {public: div (double left, double right): Operation (left, right) {try {If (Right = 0) throw runtime_error ("the divisor cannot be 0 \ n");} catch (const runtime_error & E) {cout <E. what () <Endl; throw ;}} double calculate () {return LHS/RHS ;}; // factory function operation * factoryfunction (double left, double right, char OP) {Switch (OP) {Case '+': return new Dd (left, right); break; Case '-': return new sub (left, right); break; Case '*': return New MUL (left, right ); break; Case '/': return New Div (left, right); break; default: Throw runtime_error ("Operation invalid! "); Break ;}} int main () {operation * Add = factoryfunction (11, 22, '+'); operation * sub = factoryfunction (25, 32, '-'); operation * Mul = factoryfunction (11, 11, '*'); operation * DIV = factoryfunction (50, 8 ,'/'); cout <add-> getleft () <"+" <add-> getright () <"=" <add-> calculate () <Endl; cout <sub-> getleft () <"-" <sub-> getright () <"=" <sub-> calculate () <Endl; cout <mul-> getleft () <"*" <mul-> getright () <"=" <mul-> calculate () <Endl; cout <div-> getleft () <"/" <div-> getright () <"=" <div-> calculate () <Endl; div-> setleft (40); cout <div-> getleft () <"/" <div-> getright () <"=" <div-> calculate () <Endl; // do not forget to destroy the pointer Delete add; delete sub; Delete Mul; Delete div; system ("pause"); Return 0 ;}


Running result:

The base class operation encapsulates the two operations to prevent users from directly accessing the operations. If you want to access or modify the operands, add the function interface to the base class. At the same time, we use inheritance to derive specific operators from the base class, and rewrite pure virtual functions in the base class according to the operator features. The advantage of doing so is to reduce coupling. To add a new operator to the code, you only need to inherit the new operator from the operation base class and do not need to modify the code in other derived classes. In this example, a factory function factoryfunction is used to instantiate an object. The factory function returns a base class pointer pointing to the object of the derived class, so that you can call the same function to generate different objects. Then, the calculate virtual function of the derived class is called according to the polymorphism.
See Chapter 1st of "big talk Design Patterns.

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.