In the simple factory model, a factory class is adopted to determine the specific implementation class that needs to be called, isolating the relationship between the client and the specific implementation class, thus reducing the coupling between the systems, but also brings up the problem that if there are many objects to be created, the factory class will be very large and still not conducive to later code maintenance.
Factory method Mode: An abstract product class that can derive a number of specific product classes. An abstract factory class that can derive a number of specific factory classes. Each specific factory class can only create an instance of a specific product class. |
The Factory method model
Realize:
Translate.java
Package com.devin.simplefactory; /** * @author author E-mail: [email protected] * @version creation time: April 24, 2015 PM 12:38:57 * Class description */public interface Tr anslate {public string saytxt (string text);}
Chinesetranslate.java
Package com.devin.simplefactory; /** * @author author E-mail: [email protected] * @version creation time: April 25, 2015 PM 12:28:45 * Class description */public class Chineset Ranslate implements Translate {@Overridepublic string saytxt (String txt) {return "Hello" + txt;}}
Englishtranslate.java
Package com.devin.simplefactory; /** * @author author E-mail: [email protected] * @version creation time: April 24, 2015 PM 1:18:24 * Class description */public class englishtr Anslate implements Translate {@Overridepublic string saytxt (String txt) {return "Hello" + txt;}}
Factory classes implemented in simple Factory mode
Simplefactory.java
Package com.devin.simplefactory; /** * @author author E-mail: [email protected] * @version creation time: April 25, 2015 PM 12:30:20 * Class description */public class Simplefa ctory {public Translate gettranslate (int type) {if (type==1) {return new chinesetranslate ();} else if (type==2) {return new E Nglishtranslate ();} else {return null;}}}
Client class
Client.java
Package com.devin.simplefactory; /** * @author author E-mail: [email protected] * @version creation time: April 25, 2015 PM 12:31:42 * Class description */public class Client { public static void Main (string[] args) {simplefactory simplefactory = new Simplefactory (); Translate Translate = simplefactory.gettranslate (1); System.out.println (Translate.saytxt ("Test")); translate = Simplefactory.gettranslate (2); System.out.println (Translate.saytxt ("Test"));}}
The factory method pattern, in fact, is to write a method in the factory class or replace the factory class with the interface form, and then each factory method to implement the factory method FactoryMethod Interface:
Factorymethod.java
Package Com.devin.factorymethod; /** * @author author E-mail: * @version created: April 24, 2015 PM 1:26:02 * Class description */public interface FactoryMethod { Translate FactoryMethod ();} FactoryMethod
Chinesefactorymethod.java
Package Com.devin.factorymethod; /** * @author author E-mail: * @version created: April 24, 2015 PM 1:27:45 * Class description */public class Chinesefactorymethod Implements factorymethod{@Overridepublic Translate FactoryMethod () {return new chinesetranslate ();}}
Englishfactorymethod.java
Package Com.devin.factorymethod; /** * @author author E-mail: * @version created: April 24, 2015 PM 1:27:45 * Class description */public class Englishfactorymethod Implements factorymethod{@Overridepublic Translate FactoryMethod () {return new englishtranslate ();}}
Client.java
Package Com.devin.factorymethod; /** * @author author E-mail: * @version created: April 24, 2015 PM 1:28:57 * Class description */public class Client {public static V OID Main (string[] args) {FactoryMethod FactoryMethod = new Chinesefactorymethod (); Translate Translate = Factorymethod.factorymethod (); System.out.println (Translate.saytxt ("Test"));}}
application of the factory method pattern: Spring 's IOC and DI.
Design pattern (ii) Factory method mode (FactoryMethod)-Create type