Factory mode (Factory method)
The factory model is suitable : When a large number of products need to be created, and have a common interface, can be created through the factory method mode. In the following three modes, the first if the passed string is incorrect, the object cannot be created correctly, the third is not required for the second type, so, in most cases, we will choose the third-static factory method mode.
1: normal Factory mode
is to create a factory class that creates instances of some classes that implement the same interface
example, press write a common factory method mode
First, create a common interface sender
Public interface Sender {public void Send (); }
Then each creates the implementation class
Implementing Class 1:mailsender
public class MailSender implements Sender { @Override public void Send () { System.out.println Mailsender! "); } }
Implementing Class 2:smssender
public class Smssender implements Sender { @Override public void Send () { System.out.println ("This is SMS Sen Der! "); } }
Create a common factory release sendfactory
public class Sendfactory {public Sender produce (String type) { if ("Mail". Equals (Type)) { return new Mailsen Der (); } else if ("SMS". Equals (Type)) { return new Smssender (); } else { System.out.println ("Please enter the correct type!"); return null;}} }
Test: Call the factory method, pass the condition to think which subclass to use
public class Factorytest {public static void Main (string[] args) { Sendfactory factory = new Sendfactory ();
sender Sender = factory.produce ("SMS"); Sender. Send (); } }
Output: This is SMS sender!
2: Multiple Factory method modes
Multiple factory method patterns are an improvement to the common factory method pattern, in which the object is not created correctly if a string error is passed in the normal factory method pattern, and multiple factory method patterns provide multiple factory methods to create the object individually.
Example: (The difference from the normal Factory mode calls the respective method return for the respective subclass)
public class Sendfactory {public Sender Producemail () { return new MailSender (); } Public Sender producesms () { return new Smssender (); } }
3: Static Factory method mode
The method in the above multiple factory method pattern is set to static, do not need to create the instance factory class, call directly.
public class Sendfactory {public static Sender Producemail () { return new MailSender (); } public static Sender producesms () { return new Smssender (); } }
The difference on the call
public class Factorytest {public static void Main (string[] args) { Sender sender = Sendfactory.producemail ();
sender. Send (); } }
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.
Reprint: Java design mode-factory method Mode (Factory methods)
Java design mode-factory method Mode (Factory methods)