This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

An overview of the

The Simple Factory Pattern belongs to the creation Pattern. The Method to create instances is usually Static, so it is also called Static Factory Method.

The simple factory pattern is where the factory class creates different instances based on different parameters. For example, Zhang SAN runs a kettle factory. When a customer needs a certain kettle, the customer does not need to know how the kettle is produced, but only needs to tell Zhang SAN his own kettle type, and then he can get the kettle he wants. The kettle manufacturer is the Factory, the type of kettle informed by the customer is the parameter, and the kettle produced by the kettle manufacturer is the Product.

Note:

  1. The method for creating an instance in a factory is usually static, but may not be.
  2. Instances that are created usually have a common parent class, but can be omitted.
  3. The simple factory pattern is not a GoF (Gang of Four) design pattern, probably because it violates the open close principle.

structure

  • Factory: The Factory class, the core of the simple Factory pattern, is responsible for creating concrete products that can be called directly from the outside. It is possible, but not recommended, to merge it with an abstract product class.
  • Product: Abstract Product class. In Java, it is an interface or abstract class. It is the parent of all concrete products and is used to describe common properties of concrete products.
  • ConcreteProduct: ConcreteProduct class, which is the creation target of the factory class and needs to implement abstract methods defined by the abstract product class.

advantages

  1. Eliminates the need for clients to create specific products directly. Because of the logical judgments contained in the factory class, which specific product instances are created and when.
  2. The client only needs to be informed of the requirements (parameters) and does not need to know the details of creating the specific product (the class name of the specific product).
  3. By introducing configuration files, new concrete product classes can be replaced and added without modifying the client.

disadvantages

  1. The factory class centralizes the creation logic for all products and is so overloaded that an exception can affect the entire system.
  2. It increases the number of classes in the system (factory classes and abstract product classes), increasing the complexity and difficulty of understanding the system, which is a common problem of design patterns.
  3. It is difficult to expand the system, which violates the open and close principle. Once new products need to be added, the logic of the factory class has to be modified, which will cause the logic to be too complicated when there are more product types.
  4. The methods that create instances in factory classes are usually static and do not form an inheritance-based hierarchy.

Application scenarios

  1. The factory class is responsible for creating fewer pairs of objects;
  2. The client only knows the parameters passed into the factory class and does not care how the object is created.

Applications in the JDK

The simple factory pattern is ubiquitous in JDK source code, such as java.util.calendar.

DateFormat is the factory class, which is merged with the abstract product class, BuddhistCalendar, JapaneseImperialCalendar, GregorianCalendar.