I. simple factory mode is also called static factory method. It is not one of the 23 design modes described by gof, but it is one of the methods we often use in the coding process.
1. The static factory method uniformly manages object creation.
The static factory method uses input parameters to determine which product instance to create, encapsulates the creation of objects, and the client only consumes the product. This separates the responsibility (module.
2. The static factory method delays product instantiation.
You can use the xml configuration file to change the product instance to another product instance without re-compiling the code.
2. There are still shortcomings in the simple factory model. The factory method model and the abstract factory model are the improvements to these shortcomings. After talking about these three modes, we will have a comparison. The following uses Nokia mobile phone as an example to describe the source code prototype in a simple factory model.
/* Nokia abstract product */<br/> # pragma once <br/> # include <string> <br/> # include <iostream> <br/> class cnokia <br />{< br/> public: <br/> cnokia (void); <br/> Public: <br/> virtual ~ Cnokia (void); <br/> Public: <br/> virtual bool makecall (const STD: string & number) const = 0; <br/> }; <br/> # include "Nokia. H "</P> <p> cnokia: cnokia (void) <br/>{< br/>}</P> <p> cnokia ::~ Cnokia (void) <br/>{< br/>}</P> <p> # pragma once <br/> # include "Nokia. H "</P> <p> class cn96: Public cnokia <br/>{< br/> Public: <br/> cn96 (void); <br/> public: <br/> virtual ~ Cn96 (void); <br/> Public: <br/> virtual bool makecall (const STD: string & number) const; <br/> }; </P> <p>/* n96 Nokia mobile phone product */<br/> # include "n96.h" </P> <p> cn96: cn96 (void) <br/>{< br/> STD: cout <"produce an n96 product" <STD: Endl; <br/>}</P> <p> cn96 ::~ Cn96 (void) <br/>{< br/>}</P> <p> bool cn96: makecall (const STD: string & number) const <br/> {<br/> STD: cout <"I am using n96. the dialing number is:" <number. c_str () <STD: Endl; <br/> return false; <br/>}</P> <p> # pragma once <br/> # include "Nokia. H "</P> <p> class cn95: Public cnokia <br/>{< br/> Public: <br/> cn95 (void); <br/> public: <br/> virtual ~ Cn95 (void); <br/> Public: <br/> virtual bool makecall (const STD: string & number) const; <br/> }; </P> <p>/* N95 Nokia mobile phone product */<br/> # include "n95.h" </P> <p> cn95: cn95 (void) <br/>{< br/> STD: cout <"produce an N95 instance" <STD: Endl; <br/>}</P> <p> cn95 ::~ Cn95 (void) <br/>{< br/>}</P> <p> bool cn95: makecall (const STD: string & number) const <br/> {<br/> STD: cout <"I am using N95. the dialing number is:" <number. c_str () <STD: Endl; <br/> return false; <br/>}</P> <p>/* n85, n81 ,... */</P> <p>/* factory class */<br/> # pragma once <br/> # include "n96.h" <br/> # include "n95.h" <br/> # include "n85.h" <br/> # include "n81.h" <br/> # include <cassert> <br/> class cnokiasimplefactory <br/> {<br /> Publ IC: <br/> cnokiasimplefactory (void); <br/> Public: <br/> ~ Cnokiasimplefactory (void); <br/> Public: <br/>/* Static factory Method */<br/> static cnokia * createnokia (const STD: string & model ); <br/>}; </P> <p> # include "nokiasimplefactory. H "</P> <p> cnokiasimplefactory: cnokiasimplefactory (void) <br/>{< br/>}</P> <p> cnokiasimplefactory ::~ Cnokiasimplefactory (void) <br/>{< br/>}</P> <p> cnokia * cnokiasimplefactory: createnokia (const STD: string & Model) <br/>{< br/> If (model = "n96") <br/>{< br/> return New cn96 (); <br/>}< br/> else if (model = "N95") <br/>{< br/> return New cn95 (); <br/>}< br/> else if (model = "n85") <br/>{< br/> return New cn85 (); <br/>}< br/> else if (model = "n81") <br/>{< br/> return New cn81 (); <br/>}< br/> else <br/>{< br/> assert (false); <br/>}< br/> return NULL; <br/>}</P> <p>/* main method, which can be viewed as a client */<br/> # include "stdafx. H "<br/> # include" nokiasimplefactory. H "</P> <p> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> cnokia * Nokia = NULL; <br/>/* <br/> modename can be read from an external XML file, dynamically <br/> determines the type of mobile phone to be created during running <br/> */<br/> STD: String modename = "n96 "; <br/> Nokia = cnokiasimplefactory: createnokia (modename); <br/> Nokia-> makecall ("123456789"); <br/> Delete Nokia; <br/> Nokia = NULL; <br/> return 0; <br/>}</P> <p>