Big talk design mode C + +-factory method mode

Source: Internet
Author: User

Factory method mode is based on a simple factory model, if you do not understand the simple factory model of students can first browse "big talk design mode C + + version-simple Factory mode." In the simple factory model, the defect of the simple factory model, which violates the development-closure principle, is mainly due to the use of the switch's judgment structure, which changes the code of the simple factory class to modify or add the new object, and does not conform to the open-close principle, So how does the factory approach model improve in that respect? We still take the example of the Add and subtract calculator in the simple factory model.

1. IOperation interface and implementation objects (Coperation_add and coperation_dec) that keep simple Factory mode

Class Ioperation{public:ioperation (): m_nnuml (0), M_NNUMR (0) {}virtual~ioperation () {}virtualvoidsetnum (int nNuml = 0, int NNUMR = 0) {m_nnuml = NNUML;M_NNUMR = NNUMR;} Virtualintcalculateresult () = 0;protected:intm_nnuml, m_nnumr;}; Class Coperation_add:public Ioperation{public:intcalculateresult () {returnm_nnuml + m_nnumr;}}; Class Coperation_dec:public Ioperation{public:intcalculateresult () {returnm_nnuml-m_nnumr;}};

2. Similar to the Add and subtract calculator interface, the factory object is also dependent on the abstract interface (unlike the simple factory model)

Class Ioperationfactory{public:virtual~ioperationfactory () {}virtualioperation*createoperation () = 0;}; Class Coperationfactory_add:publicioperationfactory{public:ioperation*createoperation () {RETURNNEWCOPERATION_ADD ();}}; Class Coperationfactory_dec:publicioperationfactory{public:ioperation*createoperation () {ReturnnewCOperation_Dec ();}};

Factory interfaceioperationfactoryProvides an interface for producing ioperation compute objects, and then different factory objects produce differentioperation Calculation object (additive factory Coperationfactory_add production Coperation_add object, subtraction factory Coperationfactory_dec production Coperation_dec Object ), that is, each production object corresponds to a factory class, a factory class produces only one product, such as Wuliangye only produces liquor, and Tsingtao beer only produces beer.

3. Using factory method mode

Voidtest () {ioperationfactory*poioperationfactory = new Coperationfactory_add (); ioperation*poioperation = NULL;if (! Poioperationfactory) {return;} Poioperation = Poioperationfactory->createoperation (); if (poioperation) {Poioperation->setnum (2, 3); printf ("2 + 3 =%d\n", Poioperation->calculateresult ());  Delete poioperation; }deletepoioperationfactory;}

The factory method pattern is similar to the simple factory pattern, which is to get a factory object and then call the factory object interface to produce the Calculator object, except that a simple factory can produce a variety of Calculator objects based on the request type, and the factory method pattern factory can only be one, if you want to produce other calculator objects, That is to let Wuliangye sell beer-prostitution, the only way is to change factories to do.

So the question is, if the user wants to calculate the multiplication, the simple factory way is to add a switch to the simple factory class, and change the user to invoke the simple factory interface symbol, that is, the 2 code needs to be changed, the factory method pattern needs to add a production multiplication calculator of the factory class, while the Poioperationfactory changed to the factory new object, that is, add a code (multiplication factory Class), modify a code, in fact, does not fully conform to the root-closure principle, but the factory method of the improvement of the model is that It changes the way the simple factory class modifies the switch structure to add the multiplication factory class, and the increase (extension) is allowed, so the factory method pattern modifies the code, but the change is reduced, which is a little bit of the factory method pattern than the simple factory model of Ox X.


Big talk design mode C + +-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.