Intention of the factory model:
Defines an interface for users to create objects, so that the subclass determines which class to instantiate.Factory methodDelay the instantiation of a class to its subclass.
Structure:
Scenario:
Two mobile phone products: Nokia and MotorolaManufacturingThe two products use two creators (factory) to manufacture them. Both creators have their own concreator (similar to the production line ). Both mobile phones must implement two basic functions: Call and photo ).
Product:
/// <Summary> <br/> // product <br/> /// </Summary> <br/> public abstract class mobilephone <br/> {<br /> public abstract void call (); </P> <p> public abstract void photo (); <br/>}
Creator: DefinitionProduction LineRequired contract (product creation ):
// <summary> <br/> // creator <br/> // /</Summary> <br/> Public interface imobilephonefactory <br/> {<br/> mobilephone createmobilephone (); <br/>}
concreatecreator: Production Line (used to produce different products)
// <summary> <br/> // concreatecreator: nokiafactory <br/> /// </Summary> <br/> public class nokiafactory: imobilephonefactory <br/>{< br/> Public mobilephone createmobilephone () <br/>{< br/> return new Nokia (); <br/>}
// <summary> <br/> /// concreatecreator: export lafactory <br/> /// </Summary> <br/> public class export lafactory: imobilephonefactory <br/>{< br/> Public mobilephone createmobilephone () <br/>{< br/> return New Motorola (); <br/>}
concreateproduct: production product (manufacturing process)
<textarea readonly name="code" class="csharp">/// <Summary> <br/> // product: Nokia <br/> /// </Summary> <br/> public class Nokia: mobilephone <br/>{< br/> Public override void call () <br/>{< br/> console. writeline ("the Nokia's call function"); <br/>}</P> <p> Public override void photo () <br/> {<br/> console. writeline ("the Nokia's photo function"); <br/>}< br/>}</textarea>
<textarea readonly name="code" class="csharp">/// <Summary> <br/> // product: Motorola <br/> /// </Summary> <br/> public class MOTOROLA: mobilephone <br/>{< br/> Public override void call () <br/>{< br/> console. writeline ("the Motorola's call function"); <br/>}</P> <p> Public override void photo () <br/> {<br/> console. writeline ("the Motorola's photo function"); <br/>}< br/>}</textarea>
Next, let's take a look at how to create a product:
static void main (string [] ARGs) <br/> {<br/> imobilephonefactory factory = new motorolafactory (); <br/> // create Motorola <br/> mobilephone = factory. createmobilephone (); <br/> mobilephone. Call (); <br/> mobilephone. photo (); </P> <p> factory = new nokiafactory (); <br/> mobilephone = factory. createmobilephone (); <br/> mobilephone. call (); <br/> mobilephone. photo (); </P> <p> console. read (); <br/>}
first, we specify the factory for manufacturing products, and then use the factory to generate our products, we do not know the specific manufacturing process.
Then, we use the features of the product:
we can see that our intent is: defines an interface for creating an object ( imobilephone Factory ), let the subclass decide which class to instantiate. factory method delays the instantiation of a class to its subclass.
Well, the above is a brief Implementation of factory method partten (this is not the key ). We can see what needs to be done to implement such a model.
Is there any more elegant implementation method. In fact, using anonymous classes in Java can be easier:
Product:
/* <Br/> * product: mobilephone <br/> */<br/> public abstract class mobilephone {</P> <p> public abstract void call (); </P> <p> public abstract void photo (); <br/>}
Creator:
/* <Br/> * Creator <br/> */<br/> Public interface imobilephonefactory {<br/> mobilephone createmobilephone (); <br/>}
Concreatcreator andConcreatproduct: We can combine the factories and manufacturing processes into one using the anonymous classes in Java.
<textarea readonly name="code" class="java">Public class Nokia extends mobilephone {</P> <p> @ override <br/> Public void call () {<br/> system. out. println ("the Nokia's call function"); </P> <p >}</P> <p> @ override <br/> Public void photo () {<br/> system. out. println ("the Nokia's photo function"); <br/>}</P> <p> private Nokia () {}</P> <p> Public static imobilephonefactory factory = <br/> New imobilephonefactory () {</P> <p> @ override <br/> Public mobilephone createmobilephone () {<br/> return new Nokia (); <br/>}< br/>}; </P> <p>}</textarea>
<textarea readonly name="code" class="java">Public class Motorola extends mobilephone {</P> <p> @ override <br/> Public void call () {<br/> system. out. println ("the Motorola's call function"); </P> <p >}</P> <p> @ override <br/> Public void photo () {<br/> system. out. println ("the Motorola's photo function"); <br/>}</P> <p> private Motorola () {}</P> <p> Public static imobilephonefactory factory = <br/> New imobilephonefactory () {</P> <p> @ override <br/> Public mobilephone createmobilephone () {<br/> return New Motorola (); <br/>}< br/>}; </P> <p >}< br/></textarea>
We can see that after providing a specific construction process, the anonymous class is used to provide the factory for the production process. The factory returns the production process. Make sure that the manufacturing process is only accessible to the factory, because the constructor is set to private. This provides a more convenient and elegant implementation in terms of syntax, and also reflects the object-oriented principle with a single responsibility. A class includes a responsibility: to create a product that should be manufactured, and to provide external services with a specified interface (factory.
Next, let's take a look at how we provide external services?
Public static void mobilemaker (imobilephonefactory factory) {<br/> mobilephone = factory. createmobilephone (); <br/> mobilephone. call (); <br/> mobilephone. photo (); <br/>}
We have created a server to provide the dependency-free service:
Public static void mobileservicewith (imobilephonefactory factory) {<br/> mobilephone = factory. createmobilephone (); <br/> mobilephone. call (); <br/> mobilephone. photo (); <br/>}
You only need to provide the specific factory (concreatecreator) for creating the service, you can create the product and make it provide the service:
/** <Br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> system. out. println ("Nokia factory to make Nokia and service:"); <br/> // make Nokia <br/> mobileservicewith (Nokia. factory); <br/> system. out. println (); <br/> system. out. println ("Motorola factory to make Motorola and service:"); <br/> // make Motorola <br/> mobilemaker (Motorola. factory); <br/>}
Call result: