Factory method mode (C ++ description)

Source: Internet
Author: User

The factory method mode is an improvement on the simple factory mode. First look at the shortcomings of the simple factory model.
Software is different from programs because it can be used by people and indirectly create benefits. Demand is the core of software development. Ignoring the needs of users, the software itself has no value.

If Nokia has developed another new mobile phone n99, our approach for systems designed in a simple factory model is:
1. added a cn99 class inherited from the cnokia abstract class, which complies with the OCP principle.
Note: OCP: open-closed principle. OCP focuses on flexibility. modifications are made by adding code, rather than modifying existing code. It is open to extensions and closed to modifications.


2. Modify the judgment logic in the static factory method and add the n99 creation code. This apparently violates OCP.

To improve the simple factory model, we must overcome the shortcomings in step 2.


In the following factory method mode, a n96 factory class is added. The existing code does not need to be modified. The OCP principle is fully supported, and the factory method mode fully complies with the Lee's replacement principle, the LSP principle is an important principle for OCP to become possible.
Note: liskov substitution principle (Rishi replacement principle): Subtypes must be able to replace their base types.

 

Although the factory method mode is designed to improve the disadvantages of the simple factory mode, this disadvantage is the violation of the software design principle OCP. However, it is not the factory method model that is better than the simple factory model, but the latter is more in line with the conventional software design philosophy.

/* Abstract factory class */<br/> # pragma once <br/> # include "Nokia. H "<br/> class cfactorymethod <br/>{< br/> Public: <br/> cfactorymethod (void); <br/> Public: <br/> virtual ~ Cfactorymethod (void); <br/> Public: <br/> virtual cnokia * createnokiamobile (void) = 0; <br/> }; </P> <p> # include "factorymethod. H "</P> <p> cfactorymethod: cfactorymethod (void) <br/>{< br/>}</P> <p> cfactorymethod ::~ Cfactorymethod (void) <br/>{< br/>}</P> <p>/* production n99 factory class */<br/> # pragma once <br/> # include "factorymethod. H "<br/> # include" n96.h "<br/> class cn96factory: <br/> Public cfactorymethod <br/>{< br/> public: <br/> cn96factory (void); <br/> Public: <br/> virtual ~ Cn96factory (void); <br/> Public: <br/> virtual cnokia * createnokiamobile (void); <br/> }; </P> <p> # include "n96factory. H "</P> <p> cn96factory: cn96factory (void) <br/>{< br/>}</P> <p> cn96factory ::~ Cn96factory (void) <br/>{< br/>}</P> <p> cnokia * cn96factory: createnokiamobile (void) <br/>{< br/> return New cn96 (); <br/>}</P> <p>/* The client creates a Nokia mobile phone at the n99 factory. */<br/> # include "stdafx. H "<br/> # include" n96factory. H "<br/> int _ tmain (INT argc, _ tchar * argv []) <br/> {<br/> cfactorymethod * factorymethod = new cn96factory (); <br/> cnokia * Nokia = factorymethod-> createnokiamobile (); <br/> Nokia-> makecall ("1234567"); <br/> return 0; <br/>}

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.