Factory method pattern of Java design pattern

1, Overview

Factory is the place where products are produced.

Using the concept of factory in Java design pattern is the place to generate objects.

Why add a factory class to an object that can be created directly?

This requires us to understand what problem the factory method is to solve. If there is only one class, we can directly new an object, which is the simplest; However, if there are multiple classes, and these classes need to create different objects for different situations, we need a factory. We can create specific objects in the factory according to conditions.

In this way, the caller is coupled with the specific target class. The caller doesn't know which object to create at all. It just puts forward conditions, and then the factory can decide which object to create according to the given conditions.

2, Simple factory method model

To say factory method mode, we have to understand the simple engineering method mode first. This mode is not the content of 23 design modes.

The so-called simple factory method mode is to create a factory for the target class. When multiple goals are achieved, make logical judgment within the factory to create different target instances according to conditions.

Let's take an example. I'll take the table as an example:

Table interface: Desk

/**
 * Table interface
 */
public interface Desk {
    String getType();
}

Wooden table: WoodenDesk

/**
 * Wooden table
 */
public class WoodenDesk implements Desk{
    private String type = "Wooden table";
    @Override
    public String getType() {
        return type;
    }
}

Plastic table: PlasticDesk

/**
 * Plastic table
 */
public class PlasticDesk implements Desk {
    private String type = "Plastic table";
    @Override
    public String getType() {
        return type;
    }
}

Type enumeration: type

/**
 * type
 */
public enum Type {
    PLASTIC,WOODEN;
}

Table factory: DeskFactory

/**
 * Table factory
 */
public class DeskFactory {
    public static Desk createDesk(Type type) {
        switch (type) {
            case WOODEN:
                return new WoodenDesk();
            case PLASTIC:
                return new PlasticDesk();
            default:
                return null;
        }
    }
}

Test class: client

/**
 * Class test
 */
public class Clineter {
    public static void main(String[] args) {
        Desk desk = DeskFactory.createDesk(Type.PLASTIC);
        System.out.println(desk.getType());
    }
}

results of enforcement

Plastic table

This is the simple factory method. There is only one factory class to realize multiple goals. When the number of target implementations increases, we have to modify the methods of the factory class to make them compatible with the new implementation types, which obviously violates the opening and closing principle, so the factory method mode appears.

3, Factory method model

Factory method pattern is an abstract upgrade of simple factory pattern, which abstracts the concept of factory into an interface, and then creates a factory implementation for each target implementation class, one-to-one implementation. When a target implementation is added, just add a factory implementation at the same time.

Here are some examples:

Table interface: Desk

/**
 * Table interface
 */
public interface Desk {
    String getType();
}

Wooden table: WoodenDesk

/**
 * Wooden table
 */
public class WoodenDesk implements Desk{
    private String type = "Wooden table";
    @Override
    public String getType() {
        return type;
    }
}

Plastic table: PlasticDesk

/**
 * Plastic table
 */
public class PlasticDesk implements Desk {
    private String type = "Plastic table";
    @Override
    public String getType() {
        return type;
    }
}

Table factory interface: DeskFactory

/**
 * Table factory interface
 */
public interface DeskFactory {
    Desk createDesk();
}

Wooden table factory: WoodenDeskFactory

/**
 * Wooden table factory
 */
public class WoodenDeskFactory implements DeskFactory{
    @Override
    public Desk createDesk(){
        return new WoodenDesk();
    }
}

Plastic table factory:

/**
 * Plastic table factory
 */
public class PlasticDeskFactory implements DeskFactory {
    @Override
    public Desk createDesk() {
        return new PlasticDesk();
    }
}

Test class: client

/**
 * Test class
 */
public class Clienter {
    public static void main(String[] args) {
        DeskFactory factory = new WoodenDeskFactory();
        Desk desk = factory.createDesk();
        System.out.println(desk.getType());
    }
}

Execution result:

Wooden table

4, Analysis

It is easy to see from the above example that the factory method pattern focuses on the factory interface.

The goal can be expanded infinitely, and the factory class should be expanded accordingly. One-to-one exists, which meets the opening and closing principle. However, if the goal is achieved more, the factory implementation class will also increase, which is not concise.

MyBatis is widely used. Both the transaction module and the data source module use the factory method pattern.

Keywords: Design Pattern

Added by jwilley on Sat, 12 Feb 2022 06:20:47 +0200