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/>}