Design pattern (ii) Factory method mode (FactoryMethod)-Create type

Source: Internet
Author: User

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

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.