Factory method mode

Source: Internet
Author: User

[1] What is the factory method model?

Define a factory interface for creating product objects, and postpone the actual creation to the subclass. The core factory category is no longer responsible for product creation. In this way, the core class becomes an abstract factory role and is only responsible for the interfaces that must be implemented by specific factory subclass, the advantage of further abstraction is that the factory method mode enables the system to introduce new products without modifying the specific factory role.

[2] sample code of factory method mode

  1 #include <iostream>  2 #include <string>  3 using namespace std;  4   5 class Operation  6 {  7 public:  8     double numberA;  9     double numberB; 10  11     virtual double getResult() 12     { 13         return 0; 14     } 15 }; 16  17 class addOperation : public Operation 18 { 19     double getResult() 20     { 21         return numberA + numberB; 22     } 23 }; 24  25   26 class subOperation : public Operation 27 { 28     double getResult() 29     { 30         return numberA - numberB; 31     } 32 }; 33  34 class mulOperation : public Operation 35 { 36     double getResult() 37     { 38         return numberA * numberB; 39     } 40 }; 41  42 class divOperation : public Operation 43 { 44     double getResult() 45     { 46         return numberA / numberB; 47     } 48 }; 49  50 class IFactory 51 { 52 public: 53     virtual Operation *createOperation() = 0; 54 }; 55  56 class AddFactory : public IFactory 57 { 58 public: 59     static Operation *createOperation() 60     { 61         return new addOperation(); 62     } 63 }; 64  65  66 class SubFactory : public IFactory 67 { 68 public: 69     static Operation *createOperation() 70     { 71         return new subOperation(); 72     } 73 }; 74  75 class MulFactory : public IFactory 76 { 77 public: 78     static Operation *createOperation() 79     { 80         return new mulOperation(); 81     } 82 }; 83  84 class DivFactory : public IFactory 85 { 86 public: 87     static Operation *createOperation() 88     { 89         return new divOperation(); 90     } 91 }; 92  93 int main() 94 { 95     Operation *oper = MulFactory::createOperation(); 96     oper->numberA = 9; 97     oper->numberB = 99; 98     cout << oper->getResult() << endl; 99     return 0;100 }
View code

 

Good good study, day up.

Sequential selection cycle Summary

Factory method mode

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.