Java Monomer Application-Architecture Mode-03.Design Mode-01.Factory Mode

Posted by lukegw on Wed, 11 Mar 2020 23:13:53 +0100

Original address: http://www.work100.net/training/monolithic-architecture-design-patterns-factory-pattern.html
More Tutorials: Beam Cloud - Free Course

Factory Mode

Sequence Number Intratext chapters video
1 Summary -
2 Realization -

See the navigation section above for reading

1. Overview

Factory Pattern is one of the most common design patterns in Java.This type of design pattern is creative and provides the best way to create objects.

In factory mode, we do not expose creation logic to clients when creating objects and point to newly created objects by using a common interface.

Intention:

Define an interface for creating objects so that its subclasses themselves decide which factory class to instantiate, and factory mode delays its creation until the subclass.

Main Solutions:

It mainly solves the problem of interface selection.

When to use:

We explicitly plan when different instances are created under different conditions.

How to solve:

Let its subclasses implement the factory interface and return an abstract product.

Key Code:

Creation is performed in its subclasses.

Application examples:

  • You need a car that can be picked up directly from the factory, regardless of how the car was made and what was happening inside the car.
  • Hibernate swaps databases by simply changing dialects and drivers.

Advantage:

  • A caller wants to create an object by knowing its name.
  • High scalability, if you want to add a product, just extend a factory class.
  • Shield the specific implementation of the product, and the caller only cares about the interface of the product.

Disadvantages:

  • Each time you add a product, you need to add a specific class and object implementation factory, which multiplies the number of classes in the system, increases the complexity of the system to a certain extent, and also increases the dependency of the system specific classes.That's not a good thing.

Use scenarios:

  • Logger: Logs may be logged to local hard drives, system events, remote servers, etc. Users can choose where to log.
  • Database access: When the user does not know what kind of database the last system uses and the database may change.
  • Designing a framework for connecting servers requires three protocols, "POP3", "IMAP", "HTTP", which can be used as product classes to implement an interface.

Matters needing attention:

As a class creation mode, the factory method mode can be used wherever complex objects need to be generated.One thing to note is that complex objects are suitable for factory mode, whereas simple objects, especially those that can be created with new, do not require factory mode.If you use factory mode, you need to introduce a factory class, which increases the complexity of your system.

2. Implementation

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

FactoryPatternDemo, our presentation class uses ShapeFactory to get Shape objects.
It will pass information to ShapeFactory (CIRCLE / RECTANGLE / SQUARE) to get the type of object it needs.

Step 1

Create an interface.

Shape.java, code as follows:

public interface Shape {
   void draw();
}

Step 2

Create entity classes that implement interfaces.

Rectangle.java, code as follows:

public class Rectangle implements Shape {

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

Square.java, code as follows:

public class Square implements Shape {

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

Circle.java, code as follows:

public class Circle implements Shape {

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

Step 3

Create a factory that generates objects of an entity class based on the given information.

ShapeFactory.java, code as follows:

public class ShapeFactory {

   //Getting objects of shape type using getShape method
   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 objects of entity classes by passing type information.

FactoryPatternDemo.java, code as follows:

public class FactoryPatternDemo {

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

      //Get the Circle object and call its draw method
      Shape shape1 = shapeFactory.getShape("CIRCLE");

      //Call Circle's draw method
      shape1.draw();

      //Gets the Rectangle object and calls its draw method
      Shape shape2 = shapeFactory.getShape("RECTANGLE");

      //Call the draw method of Rectangle
      shape2.draw();

      //Get the Square object and call its draw method
      Shape shape3 = shapeFactory.getShape("SQUARE");

      //Call Square's draw method
      shape3.draw();
   }
}

Step 5

Execute the program and output the result:

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

Last: brief introduction
Next: Abstract Factory Mode

If you are interested in the course content, you can scan our public number or QQ group and keep an eye on our course updates


Topics: Java Database Hibernate