Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Factory mode


May 27, 2021 Design mode


Table of contents


Factory mode

Factory Pattern is one of the most commonly used design patterns in Java. This type of design pattern belongs to the creation pattern and provides the best way to create objects.

In factory mode, we create the logic without exposing the client when we create the object, and we point to the newly created object by using a common interface.

Introduced

Intent: Define an interface that creates an object and let its sub-class decide for itself which factory class to instantiate, and the factory pattern delays its creation process to the sub-class.

Main solution: Mainly solve the problem of interface selection.

When to use: We explicitly plan to create different instances under different conditions.

How to solve it: Let its subses implement the factory interface, returning an abstract product.

Key code: The creation process is performed in its sub-class.

Application example: 1, you need a car, you can pick up goods directly from the factory, without having to worry about how the car is made, and the specific implementation of the car inside. 2 , Hibernate change database only need to change dialects and drive on it.

Pros: 1, a caller wants to create an object as long as he or she knows its name. 2 , high scalability, if you want to add a product, as long as the expansion of a factory class can be. 3 , shielding the specific implementation of the product, the caller only cares about the interface of the product.

Cons: Each time you add a product, you need to add a specific class and object implementation factory, so that the number of classes in the system multiplied, to a certain extent, increased the complexity of the system, but also increased the reliance on specific classes of the system. That's not a good thing.

Scenario: 1, Logging recorder: logging may be recorded to the local hard disk, system events, remote servers, etc., the user can choose where to log. 2 , database access, when the user does not know what kind of database the last system to use, and the database may change. 3 , the design of a framework to connect servers, need three protocols, "POP3", "IMAP", "HTTP", can be these three as a product class, together to achieve an interface.

Note: As a class creation pattern, you can use the factory method pattern where you need to generate complex objects. O ne thing to note is that complex objects are suitable for factory mode, while simple objects, especially those that can be created with new, do not need to use factory mode. If you use factory mode, you need to introduce a factory class that increases the complexity of the system.

Realize

We'll create a Shape interface and the entity class that implements the Shape interface. The next step is to define the factory class ShapeFactory.

FactoryPatternDemo, our demo class uses ShapeFactory to get Shape objects. It passes information to ShapeFactory (CIRCLE / RECTANGLE / SQUARE) to get the type of object it wants.

Factory mode

Step 1

Create an interface.

Shape.java

public interface Shape {
   void draw();
}

Step 2

Create an entity class that implements the interface.

Rectangle.java

public class Rectangle implements Shape {

   @Override
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}

Square.java

public class Square implements Shape {

   @Override
   public void draw() {
      System.out.println("Inside Square::draw() method.");
   }
}

Circle.java

public class Circle implements Shape {

   @Override
   public void draw() {
      System.out.println("Inside Circle::draw() method.");
   }
}

Step 3

Create a factory that generates objects for entity classes based on given information.

ShapeFactory.java

public class ShapeFactory {
 
   //使用 getShape 方法获取形状类型的对象
   public Shape getShape(String shapeType){
      if(shapeType == null){
         return null;
      }       
      if(shapeType.equalsIgnoreCase("CIRCLE")){
         return new Circle();
      } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();
      } else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }
      return null;
   }
}

Step 4

Using this factory, you get the objects of the entity class by passing type information.

FactoryPatternDemo.java

public class FactoryPatternDemo {

   public static void main(String[] args) {
      ShapeFactory shapeFactory = new ShapeFactory();

      //获取 Circle 的对象,并调用它的 draw 方法
      Shape shape1 = shapeFactory.getShape("CIRCLE");

      //调用 Circle 的 draw 方法
      shape1.draw();

      //获取 Rectangle 的对象,并调用它的 draw 方法
      Shape shape2 = shapeFactory.getShape("RECTANGLE");

      //调用 Rectangle 的 draw 方法
      shape2.draw();

      //获取 Square 的对象,并调用它的 draw 方法
      Shape shape3 = shapeFactory.getShape("SQUARE");

      //调用 Square 的 draw 方法
      shape3.draw();
   }
}

Step 5

Verify the output.

Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.