GOF factory mode

GOF creative pattern series

  1. Singleton mode
  2. Factory mode
  3. Abstract factory pattern
  4. Builder pattern
  5. Prototype mode

reference material

  1. Shang Xuetang_ Simple factory mode
  2. Shang Xuetang_ Factory mode
  3. Rookie tutorial_ Factory mode

1. General

  • Basic principles of object-oriented design

    • OCP (open closed principle): a software entity should be open to extensions
      Release and close the modification.
    • DIP (dependency inversion principle): to program for the interface,
      Do not program against implementations.
    • LoD (Law of Demeter): only communicate with your direct friends and avoid contact with them
      Communication with strangers.
  • Function: it realizes the separation of creator and caller

  • Core essence

    • Instantiate the object and replace the new operation with the factory method
    • Implementation classes will be selected and objects created for unified management and control. This decouples the caller from the implementation class.
  • Classification and usage scenarios

    • Simple factory (static factory)
      • It is used to produce any product in the same hierarchical structure. (for adding new products, the existing code needs to be modified)
    • factory
      • It is used to produce fixed products in the same hierarchical structure. (any product can be added)
    • Abstract factory
      • Used to produce all products of different product families. (there is nothing you can do to add new products; support adding product families)

2. Simple factory

  • Simple factory classes generally use static methods to return different object instances through different received parameters.

    • You can use if ···· else to judge the passed in parameters
    • You can also write different creation methods according to different classes
  • There is nothing I can do to add new products!!! It cannot be extended without modifying the code.

  • Shape class interface and its implementation class

    public interface Shape {
       void draw();
    }
    public class Rectangle implements Shape {
       @Override
       public void draw() { System.out.println("Inside Rectangle::draw() method."); }
    }
    public class Circle implements Shape {
       @Override
       public void draw() { System.out.println("Inside Circle::draw() method."); }
    }
    
  • Simple factory

    • Implementation method 1: use if ···· else to judge the passed parameters to return different object instances
      public class ShapeFactory {
         //Use the getShape method to get an object of shape type
         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(); } 
            return null;
         }
      }
      
    • Implementation method 2: write different creation methods according to different classes to return different object instances
      public class ShapeFactory {
         public Shape getRectangle(String shapeType){ return new Rectangle(); }
         public Shape getCircle(String shapeType){ return new Circle(); }
      }
      

3. Factory mode

  • Avoid the disadvantages of simple factory mode: it does not fully meet the OCP opening and closing principle.

  • The biggest difference between factory mode and simple factory mode is:

    • The simple factory pattern has only one factory class (for a project or a separate module)
    • The factory method pattern has a set of factory classes that implement the same interface.
  • advantage:

    1. A caller wants to create an object, just know its name.
    2. High scalability. If you want to add a product, you can only extend a factory class.
    3. Mask the specific implementation of the product, and the caller only cares about the product interface.

  • Disadvantages:

    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 increases exponentially, which not only increases the complexity of the system to a certain extent, but also increases the dependence of the specific classes of the system. This is not a good thing.

  • Factory mode implementation

    /* ShapeFactory Interface file */
    public interface ShapeFactory {
    	public Shape getShape();
    }
    /* RectangleFactory.java file  */
    public class RectangleFactory implements ShapeFactory {
    	@Override
    	public Shape getShape(){ return new Rectangle(); }
    }
    /* CircleFactory.java file  */
    public class CircleFactory implements ShapeFactory {
    	@Override
    	public Shape getShape(){ return new Circle(); }
    }
    

Keywords: Java Design Pattern

Added by Jarod on Tue, 18 Jan 2022 08:09:59 +0200