Introduction of simple factory pattern

(1) First, the simple factory pattern is not one of the 23 design patterns, but it is often used as a basis for learning other factory patterns. (2) Simple factory is generally divided into: ordinary simple factory, multi-method simple factory, static method simple factory. (3) The Simple Factory Pattern defines a Factory class, which can return instances of different classes according to different parameters. The created instances usually have a common parent class. Because the methods used to create instances in the simple Factory pattern are static methods, the simple Factory pattern is also known as the static Factory Method pattern, which belongs to the class creation pattern. (4) The point of the simple factory model is that when you need something, just pass in the right argument and you can get the object you need without knowing the details of its creation.Copy the code

Two simple factory patterns in three ways

(1) plain simple factories (2) simple factories with multiple methods (3) simple factories with multiple static methodsCopy the code

2-1 Ordinary simple factory

Create a factory class that creates instances of classes that implement the same interface. Public interface Sender {public void Send(); public void Send(); } (2) Next, create two classes to implement:  public class MailSender implements Sender { @Override public void Send() { System.out.println("this is mailsender!" ); } } public class SmsSender implements Sender { @Override public void Send() { System.out.println("this is sms sender!" ); }} (3)  public class SendFactory { public Sender produce(String type) { if ("mail".equals(type)) { return new MailSender(); } else if ("sms".equals(type)) { return new SmsSender(); } else {system.out.println (" Please enter the correct type!") ); return null; (4) Test classes are as follows:  public class FactoryTest { public static void main(String[] args) { SendFactory factory = new SendFactory(); Sender sender = factory.produce("sms"); sender.Send(); }} this is SMS sender!Copy the code

2-2 Multi-method simple factory

The simple multi-method factory is an improvement over the normal factory method pattern, where objects cannot be created correctly if a string is passed in error, whereas the multi-factory method pattern provides multiple factory methods to create objects separately. Public class SendFactory {public Sender produceMail(){return new MailSender(); } public Sender produceSms(){ return new SmsSender(); (2) Test classes are as follows:  public class FactoryTest { public static void main(String[] args) { SendFactory factory = new SendFactory(); Sender sender = factory.produceMail(); sender.Send(); }} This is mailSender!Copy the code

2-1 A simple factory for multiple static methods

Make the methods in the above multiple factory method pattern static, calling them without creating an instance. Public class SendFactory {public static Sender produceMail(){return new MailSender(); } public static Sender produceSms(){ return new SmsSender(); (2) Test classes are as follows:  public class FactoryTest { public static void main(String[] args) { Sender sender =SendFactory.produceMail(); sender.Send(); }} This is mailSender!Copy the code

3 summary

In general, the factory method pattern is used when a large number of products need to be created and have common interfaces. In the above three modes: first, if the string passed in is incorrect, the object cannot be created correctly. The third method does not require the instance chemical plant class as opposed to the second, so in most cases we will choose the third ———— static factory method pattern. Problems with the simple factory pattern One problem with the simple factory pattern is that the creation of classes depends on the factory class, which means that if you want to extend the program, you have to modify the factory class, which violates the closure principle.Copy the code