Design pattern creation factory method

Time:2021-6-24

Design pattern creation factory method

introduce

Factory method is a kind of creative design pattern, which provides a method to create objects in the parent class and allows the subclass to determine the type of instantiated objects.

scene

If we develop the log module in the project, there is only console log at the beginning of the business, and most of the code is in the console log class. With the development of business, we hope to increase the file log function.

If there is already a coupling relationship between the codes, it is not easy to add a new class to the program. At present, most of the code is related to the console log class. To add a file log class, you need to modify all the code. What’s more, if you want to expand other log classes in the future, you may need to modify the code again.

Over and over again, congratulations on the birth of a non maintainable ancestral code.

Solution

The factory method pattern suggests using special factory methods instead of directly calling the new keyword to create objects. The objects returned by factory methods are often referred to as “products.”.

public abstract class Logger {
    public abstract void log();
}
public class ConsoleLogger extends Logger {
public class FileLogger extends Logger {

Then we create a factory class, which is declared as an abstract class, and set an abstract method to force the subclass to implement the abstract method. You can also set it not to be an abstract class and return the default type in the base factory method.

public abstract class LoggerFactory {
    public abstract Logger createLogger();
}
public class ConsoleLoggerFactory extends LoggerFactory {
    @Override
    public Logger createLogger() {
        return new ConsoleLogger();
    }
}
public class FileLoggerFactory extends LoggerFactory {
    @Override
    public Logger createLogger() {
        return new FileLogger();
    }
}

Design pattern creation factory method

At first glance, the factory method pattern looks pointless, but only changes the location of the calling function in the program. But we can override factory methods in subclasses to change the type of product they create.

However, it should be noted that only when these products have a common parent class or interface can factory subclasses return different types of products. At the same time, the factory method of the factory base class should declare its return type as a common interface.

As long as the product class implements a common interface, it can be passed to the customer code without providing any additional data.

public class Application {

    private final LoggerFactory loggerFactory;

    public Application(LoggerFactory loggerFactory) {
        this.loggerFactory = loggerFactory;
    }

    public void run() {
        Logger logger = loggerFactory.createLogger();
        logger.log();
    }
}

Client code only uses factories and products through abstract types. If new products need to be added, just implement factory abstract classes and product abstract classes. There is no need to modify the original factory or product class.

summary

This is a note for relearning the singleton mode. There may be many mistakes in it. You are welcome to correct them.

This work adoptsCC agreementReprint must indicate the author and the link of this article

Hello。

Recommended Today

What is “hybrid cloud”?

In this paper, we define the concept of “hybrid cloud”, explain four different cloud deployment models of hybrid cloud, and deeply analyze the industrial trend of hybrid cloud through a series of data and charts. 01 introduction Hybrid cloud is a computing environment that integrates multiple platforms and data centers. Generally speaking, hybrid cloud is […]