In Java 23, the design mode is Factory Method. The design mode is factory.
1. General factory Model
Factory
/*** @ Title Factory. java * @ Package factory. factory1 * @ date 10:16:02 * @ version V1.0 */package factory. factory1;/*** @ ClassName Factory * @ date 2015-1-22 10:16:02 */public class Factory {public Sender procedure (String type) {if ("mail ". endsWith (type) {return new MailSender ();} if ("sms ". endsWith (type) {return new SMSSender ();} else {System. out. println ("incorrect type not entered"); // if better, add an exception return null ;}}}
Transmitter Interface
/*** @ Title Sender. java * @ Package factory. factory1 * @ date 10:07:24 * @ version V1.0 */package factory. factory1;/*** @ ClassName Sender * @ date 10:07:24 */public interface Sender {public void send ();}
Transmitter implementation class
/*** @ Title MailSender. java * @ Package factory. factory1 * @ date 10:09:29 * @ version V1.0 */package factory. factory1;/*** @ ClassName MailSender * @ date 10:09:29 */public class MailSender implements Sender {/* (non-Javadoc) * @ see factory. factory1.Sender # send () */@ Override public void send () {System. out. println ("this is a mailsender ");}}
/*** @ Title SMSSender. java * @ Package factory. factory1 * @ date 10:14:36 * @ version V1.0 */package factory. factory1;/*** @ ClassName SMSSender * @ date 10:14:36 */public class SMSSender implements Sender {/* (non-Javadoc) * @ see factory. factory1.Sender # send () */@ Override public void send () {System. out. println ("this is a smssender ");}}
Test class:
/*** @ Title Test. java * @ Package factory. factory1 * @ date 10:20:19 * @ version V1.0 */package factory. factory1;/*** @ ClassName Test * @ date 10:20:19 */public class Test {public static void main (String [] args) {Factory factory = new Factory (); sender sender = factory. procedure ("mail"); sender. send ();}}
The implementation method of this method requires a high level of input type, that is, if the input parameter is incorrect, it cannot obtain the corresponding output.
2. Methods for Multiple factories
/*** @ Title Factory. java * @ Package factory. factory1 * @ date 10:16:02 * @ version V1.0 */package factory. factory3;/*** @ ClassName Factory * @ date 2015-1-22 10:16:02 */public class Factory {public Sender mailSender () {return new MailSender ();} public Sender smsSender () {return new SMSSender ();}}
Modify the factory class above to this way.
Test class:
/*** @ Title Test. java * @ Package factory. factory1 * @ date 10:20:19 * @ version V1.0 */package factory. factory3;/*** @ ClassName Test * @ date 2015-1-22 10:20:19 */public class Test {public static void main (String [] args) {Factory factory = new Factory (); sender sender = factory. mailSender (); sender. send ();}}
3. The static factory method changes the factory method to the static method.
/*** @ Title Factory. java * @ Package factory. factory1 * @ date 10:16:02 * @ version V1.0 */package factory. factory4;/*** @ ClassName Factory * @ date 2015-1-22 10:16:02 */public class Factory {public static Sender mailSender () {return new MailSender ();} public static Sender smsSender () {return new SMSSender ();}}
Test class
/*** @ Title Test. java * @ Package factory. factory1 * @ date 10:20:19 * @ version V1.0 */package factory. factory4;/*** @ ClassName Test * @ date 10:20:19 */public class Test {public static void main (String [] args) {// Factory factory = new Factory (); Sender sender = Factory. mailSender (); sender. send ();}}
4. Abstract Factory Model
One problem with the factory method mode is that the creation of the class depends on the factory class. That is to say, if you want to expand the program, you must modify the factory class, which violates the closure principle. Therefore, from the design perspective, how can we solve certain problems? The abstract factory mode is used to create multiple factory classes. Once new functions need to be added, new factory classes can be directly added without modifying the previous code. Because the abstract factory is not very easy to understand, let's first look at the figure, and then it is easier to understand with the code.
Provide a factory Interface
/*** @ Title Provider. java * @ Package factory. factory2 * @ date 10:36:18 * @ version V1.0 */package factory. factory2;/*** @ ClassName Provider * @ date 10:36:18 */public interface Provider {public Sender procedure ();}
Provides an interface for sending messages.
/*** @ Title Sender. java * @ Package factory. factory2 * @ date 10:37:03 * @ version V1.0 */package factory. factory2;/*** @ ClassName Sender * @ date 10:37:03 */public interface Sender {public void send ();}
There are two classes to implement this sending interface.
/*** @ Title MailSender. java * @ Package factory. factory2 * @ date 10:45:05 * @ version V1.0 */package factory. factory2;/*** @ ClassName MailSender * @ date 10:45:05 */public class MailSender implements Sender {/* (non-Javadoc) * @ see factory. factory2.Sender # send () */@ Override public void send () {System. out. println ("this is a mail sender ");}}
/*** @ Title SmsSender. java * @ Package factory. factory2 * @ date 10:40:06 * @ version V1.0 */package factory. factory2;/*** @ ClassName SmsSender * @ date 10:40:06 */public class SmsSender implements Sender {/* (non-Javadoc) * @ see factory. factory2.Sender # send () */@ Override public void send () {System. out. println ("this is a mail sender ");}}
Provides two classes to implement the abstract factory Interface
/*** @ Title SendMailFactory. java * @ Package factory. factory2 * @ date 10:44:34 * @ version V1.0 */package factory. factory2;/*** @ ClassName SendMailFactory * @ date 10:44:34 */public class SendMailFactory implements Provider {/* (non-Javadoc) * @ see factory. factory2.Provider # procedure () */@ Override public Sender procedure () {// TODO Auto-generated method stub return new MailSender ();}}
/*** @ Title SmsSendFactory. java * @ Package factory. factory2 * @ date 10:41:23 * @ version V1.0 */package factory. factory2;/*** @ ClassName SmsSendFactory * @ date 10:41:23 */public class SmsSendFactory implements Provider {/* (non-Javadoc) * @ see factory. factory2.Provider # procedure () */@ Override public Sender procedure () {// TODO Auto-generated method stub return new SmsSender ();}}
Test class:
/*** @ Title Test. java * @ Package factory. factory2 * @ date 10:48:29 * @ version V1.0 */package factory. factory2;/*** @ ClassName Test * @ date 10:48:29 */public class Test {public static void main (String [] args) {Provider provider = new SendMailFactory (); sender sender = provider. procedure (); sender. send ();}}