Simple Factory Pattern of Design Patterns

factory pattern

  • Tell the factory class what I need, and the factory class will provide me with what; the factory pattern is a typical interface-oriented programming, which minimizes the coupling of coding.

Simple Factory Pattern

The simple factory pattern is relatively simple, and it is also a design pattern that may be often used in projects. For example, when we are developing, we will have different libraries, such as: offline development environment database, pre-release environment database, and production environment database. If we do not use the factory mode, we need to manually change the relevant database connection code every time we switch the environment. At this time, the code has serious coupling. And we can greatly reduce this coupling by using the simple factory pattern. The code written is as follows:

Define an interface to connect to the database

/**
 *  定义一个连接数据库的接口。
 * @author wushuaiping
 * @date 2018/3/11 下午2:09
 */
public interface DatabaseConnection {
    /**
     *  连接数据库。
     */
    void connection();
}

Then write database connection code for different environments to implement it

Implementation class for connecting offline environment database

/**
 *  连接线下环境数据库
 * @author wushuaiping
 * @date 2018/3/11 下午2:34
 */
public class DailyConnection implements DatabaseConnection {
    public void connection() {
        // 这里是连接线下数据库的相关逻辑
        System.out.println("日常环境连接成功...");
    }
}

Implementation class for connecting to the pre-release environment database

/**
 *  连接预发环境数据库
 * @author wushuaiping
 * @date 2018/3/11 下午2:39
 */
public class PreConnection implements DatabaseConnection {
    public void connection() {
        // 连接预发环境数据库的逻辑代码
        System.out.println("预发环境连接成功...");
    }
}

Implementation class for connecting to online environment database

/**
 * 连接线上环境数据库
 * @author wushuaiping
 * @date 2018/3/11 下午2:42
 */
public class ProConnection implements DatabaseConnection {
    public void connection() {
        // 连接线上环境数据库的逻辑
        System.out.println("线上环境连接成功...");
    }
}

Write a factory class


/**
 * 数据库环境选择工厂类
 * @author wushuaiping
 * @date 2018/3/11 下午2:43
 */
public class ConnectionFactory {

    public static final String ENV = "env";

    public static final String PRE = "pre";

    public static final String PRO = "pro";

    /**
     * 选择连接数据库
     * @param env
     * @return true: 连接成功
     *         false: 连接未成功
     */
    public boolean connection(String env){
        DatabaseConnection databaseConnection;
        try {
            if (ENV.equals(env)){
                databaseConnection = new DailyConnection();
                databaseConnection.connection();
            }else if (PRE.equals(env)){
                databaseConnection = new PreConnection();
                databaseConnection.connection();
            }else if (PRO.equals(env)){
                databaseConnection = new ProConnection();
                databaseConnection.connection();
            }
        }catch (Exception e){
            return false;
        }
        return true;
    }
}

Test Case

public class Main {
    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        // 日常
        factory.connection(ConnectionFactory.ENV);
        // 预发
        factory.connection(ConnectionFactory.PRE);
        // 线上
        factory.connection(ConnectionFactory.PRO);
    }
}

output

日常环境连接成功...
预发环境连接成功...
线上环境连接成功...

If you write code in this way, if you add a new database environment in the future, you only need to add a new connection class and add relevant code to the factory class, without changing the code logic in other environments. Study notes, if there is something wrong, please give pointers and criticisms from all walks of life. O(∩_∩)O Thank you

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325902828&siteId=291194637