This is the fourth day of my participation in the November Gwen Challenge. See details: The last Gwen Challenge 2021.

The preface

The design pattern we’ll look at today is called the Simple Factory pattern, which solves the problem of complex object generation when the constructor of a class has too many parameters and the parameters affect the behavior of the class.

Introduction to the simple factory pattern

The simple Factory pattern is not really a design pattern, but more of a programming habit. It mainly facilitates the creation of classes by defining a factory class that returns different instances according to the parameters passed in, and the created instances have a common parent class or interface.

Applicable scenario

  1. There are fewer objects to create.
  2. You don’t care about the object creation process.

Because the factory pattern’s classes are pre-designed, creating too many objects can be very bloated and complex, and since the factory encapsulates the object creation process, the object creation process is not easy to interfere with.

Roles in the simple Factory pattern

  1. SimpleFactory: is the core of the SimpleFactory pattern and is responsible for implementing the internal logic that creates all instances. The method of creating a product class for a factory class can be called directly from the outside world to create the desired product object.
  2. Abstract Product: Is the parent of all objects created by a simple factory and is responsible for describing the common interface shared by all instances.
  3. Concrete Products: Are the creation targets of the simple factory pattern.

Take a look at the image above:

  1. First we have a production class factory, and the abstract product defines the goal that we want to produce.
  2. Then implement concrete product methods that can produce products that conform to the abstract.
  3. The final process is that the factory accepts the parameters, selects the production method for the concrete product, and then comes up with a result that conforms to the abstract product.

The actual case

Let’s look at an example that follows the pattern shown above.

Public class Client {public static void main(String[] args) {} public interface Product {void show(); } // ProductA static class ConcreteProduct1 implements Product {public void show() {console.log(" ConcreteProduct1 implements..."); ); }} // ProductB static class ConcreteProduct2 implements Product {public void show() {console.log(" ConcreteProduct2 implements Product ") {console.log(" ConcreteProduct2 implements Product "); ); }} // ProductC static class ConcreteProduct3 implements Product {public void show() {console.log(" ConcreteProduct3 implements Product ") {public void show() {console.log(" ConcreteProduct3 implements Product "); ); } } class Const { static PRODUCT_A = 0; static PRODUCT_B = 1; static PRODUCT_C = 2; } static class SimpleFactory { public static Product makeProduct(int kind) { switch (kind) { case Const.PRODUCT_A: return new ConcreteProduct1(); case Const.PRODUCT_B: return new ConcreteProduct2(); case Const.PRODUCT_C: return new ConcreteProduct3(); } return null; }}}Copy the code

Conclusion:

Advantages:

  1. The factory class contains the logical judgment necessary to determine which instance of a product to create and when. The client can be relieved of the responsibility of creating product objects directly, making it easy to create corresponding products. The responsibilities of plant and product are clearly differentiated.
  2. The client does not need to know the class name of the specific product being created, just the parameters.
  3. Configuration files can also be introduced to replace and add new concrete product classes without modifying the client code.

Disadvantages:

  1. The simple factory mode has a single factory class, which is responsible for the creation of all products. The responsibility is too heavy. Once an exception occurs, the whole system will be affected. And the factory class code will be very bloated, against the principle of high aggregation.
  2. Using the simple factory pattern increases the number of classes in the system (introducing new factory classes), increasing the complexity and difficulty of understanding the system
  3. It is difficult to expand the system. Once a new product is added, the factory logic has to be modified. The logic may be too complicated when there are many product types
  4. The simple factory pattern uses the static factory approach, which prevents the factory roles from forming an inheritance-based hierarchy.