Java 23 Design Pattern-factory method mode (Factory)

Source: Internet
Author: User

1. General Factory mode

Factory class

/*** @Title Factory.java * @Package factory.factory1 * @date 2015-1-22 Morning 10:16:02 *@versionV1.0*/ PackageFactory.factory1;/*** @ClassName Factory * @date 2015-1-22 a.m. 10:16:02*/ Public classFactory { PublicSender procedure (String type) {if("Mail". EndsWith (Type)) {            return NewMailSender (); }        if("SMS". EndsWith (Type)) {            return NewSmssender (); }Else{System.out.println ("The correct type was not entered"); //better to add exceptions            return NULL; }    }}

Transmitter interface

/**@version* * package factory.factory1; /** * @ClassName Sender * @date 2015-1-22 a.m. 10:07:24   */  Public Interface Sender {publicvoid  Send ();}

Transmitter Implementation Class

/*** @Title Mailsender.java * @Package factory.factory1 * @date 2015-1-22 Morning 10:09:29 *@versionV1.0*/ PackageFactory.factory1;/*** @ClassName MailSender * @date 2015-1-22 a.m. 10:09:29*/ Public classMailSenderImplementssender{/*(non-javadoc) * @see factory.factory1.sender#send ()*/@Override Public voidSend () {System.out.println ("This is a MailSender"); }}
/*** @Title Smssender.java * @Package factory.factory1 * @date 2015-1-22 Morning 10:14:36 *@versionV1.0*/ PackageFactory.factory1;/*** @ClassName Smssender * @date 2015-1-22 a.m. 10:14:36*/ Public classSmssenderImplementssender{/*(non-javadoc) * @see factory.factory1.sender#send ()*/@Override Public voidSend () {System.out.println ("This is a Smssender"); }}

Test class:

/*** @Title Test.java * @Package factory.factory1 * @date 2015-1-22 Morning 10:20:19 *@versionV1.0*/ PackageFactory.factory1;/*** @ClassName Test * @date 2015-1-22 morning 10:20:19*/ Public classTest { Public Static voidMain (string[] args) {Factory Factory=NewFactory (); Sender Sender= Factory.procedure ("Mail"); Sender.send ();}}

This method of implementation of the incoming type requirements are relatively high, that is, if the parameters passed in the error will not be able to obtain the corresponding generation of things.

2. Methods for multiple factories

/*** @Title Factory.java * @Package factory.factory1 * @date 2015-1-22 Morning 10:16:02 *@versionV1.0*/ PackageFactory.factory3;/*** @ClassName Factory * @date 2015-1-22 a.m. 10:16:02*/ Public classFactory { PublicSender MailSender () {return NewMailSender (); }         PublicSender Smssender () {return NewSmssender (); }}

Modify the factory class above to do this.

Test class:

/*** @Title Test.java * @Package factory.factory1 * @date 2015-1-22 Morning 10:20:19 *@versionV1.0*/ PackageFactory.factory3;/*** @ClassName Test * @date 2015-1-22 morning 10:20:19*/ Public classTest { Public Static voidMain (string[] args) {Factory Factory=NewFactory (); Sender Sender=Factory.mailsender (); Sender.send ();}}

3. Static factory methods modify the factory method to a static method

/*** @Title Factory.java * @Package factory.factory1 * @date 2015-1-22 Morning 10:16:02 *@versionV1.0*/ PackageFactory.factory4;/*** @ClassName Factory * @date 2015-1-22 a.m. 10:16:02*/ Public classFactory { Public StaticSender MailSender () {return NewMailSender (); }         Public StaticSender Smssender () {return NewSmssender (); }}

Test class

/*** @Title Test.java * @Package factory.factory1 * @date 2015-1-22 Morning 10:20:19 *@versionV1.0*/ PackageFactory.factory4;/*** @ClassName Test * @date 2015-1-22 morning 10:20:19*/ Public classTest { Public Static voidMain (string[] args) {//Factory Factory = new Factory ();Sender sender =Factory.mailsender (); Sender.send ();}}

4. Abstract Factory mode

The problem with factory method mode is that class creation relies on the factory class, that is, if you want to expand the program, the factory class must be modified, which violates the closure principle, so from the design point of view, there are certain problems, how to solve? Use the abstract factory pattern to create multiple factory classes so that once you need to add new functionality, you can add new factory classes directly, without having to modify the previous code. Because the abstract factory is not very well understood, we first look at the diagram, and then the code, it is more easily understood.

Provides an interface to a factory

/**@version* * package Factory.factory2; /** * @ClassName Provider * @date 2015-1-22 a.m. 10:36:18   */  Public Interface Provider { public Sender procedure ();}

Provides an interface for sending

/**@version* * package Factory.factory2; /** * @ClassName Sender * @date 2015-1-22 a.m. 10:37:03   */  Public Interface Sender {publicvoid  Send ();}

Two classes to implement this send interface

/*** @Title Mailsender.java * @Package factory.factory2 * @date 2015-1-22 Morning 10:45:05 *@versionV1.0*/ PackageFactory.factory2;/*** @ClassName MailSender * @date 2015-1-22 a.m. 10:45:05*/ Public classMailSenderImplementssender{/*(non-javadoc) * @see factory.factory2.sender#send ()*/@Override Public voidSend () {System.out.println ("This is a mail sender"); }}
/*** @Title Smssender.java * @Package factory.factory2 * @date 2015-1-22 Morning 10:40:06 *@versionV1.0*/ PackageFactory.factory2;/*** @ClassName Smssender * @date 2015-1-22 a.m. 10:40:06*/ Public classSmssenderImplementssender{/*(non-javadoc) * @see factory.factory2.sender#send ()*/@Override Public voidSend () {System.out.println ("This is a mail sender"); }}

Provides two classes for implementing an abstract factory interface

/*** @Title Sendmailfactory.java * @Package factory.factory2 * @date 2015-1-22 Morning 10:44:34 *@versionV1.0*/ PackageFactory.factory2;/*** @ClassName sendmailfactory * @date 2015-1-22 a.m. 10:44:34*/ Public classSendmailfactoryImplementsProvider {/*(non-javadoc) * @see factory.factory2.provider#procedure ()*/@Override PublicSender procedure () {//TODO auto-generated Method Stub        return NewMailSender (); }}
/*** @Title Smssendfactory.java * @Package factory.factory2 * @date 2015-1-22 Morning 10:41:23 *@versionV1.0*/ PackageFactory.factory2;/*** @ClassName smssendfactory * @date 2015-1-22 a.m. 10:41:23*/ Public classSmssendfactoryImplementsprovider{/*(non-javadoc) * @see factory.factory2.provider#procedure ()*/@Override PublicSender procedure () {//TODO auto-generated Method Stub        return NewSmssender (); }}

Test class:

/*** @Title Test.java * @Package factory.factory2 * @date 2015-1-22 Morning 10:48:29 *@versionV1.0*/ PackageFactory.factory2;/*** @ClassName Test * @date 2015-1-22 morning 10:48:29*/ Public classTest { Public Static voidMain (string[] args) {Provider Provider=Newsendmailfactory (); Sender Sender=provider.procedure (); Sender.send ();}}

23 Design Patterns in Java-factory method mode (Factory)

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.