I heard that wechat search “Java fish” will change strong oh!

This article is posted on Github and Gitee, and contains my entire Java series of articles for study and interview purposes

(A), what is the factory method mode

There is a general factory responsible for the production of various TV products, when there is a demand for the production of Mi TV, a factory will be allocated to make mi TV, and then the factory can be used to produce Mi TV. The above way of instantiating classes through factories is abstracted into the design pattern as the factory method pattern.

(2) The role of factory method pattern

There are four roles in the factory method pattern, and the factory method pattern can be understood by understanding these four roles.

Abstract Factory: The general Factory in this example, the abstract Factory is the base class for all concrete factories. In the abstract Factory role, the method that generates the product instance is declared. The purpose of the abstract factory is to allow instance generation not through New, but through a specialized method for instance generation

ConcreteFactory: The ConcreteFactory that makes Mi TV in this example, is a subclass of abstract factory that implements the method of generating product instances defined in abstract factory.

Abstract Product: The TV Product in this example is a total abstract Product that defines a set of methods for a Product.

ConcreteProduct: in this example, mi TV is a ConcreteProduct, a method to realize abstract products.

(3) Factory method pattern code cases

The following code describes an example of the factory method pattern that implements the Scene Abstract TV Factory class from Chapter 1 of this article, providing an abstract method for produceTv.

public abstract class TvFactory {
    public abstract Television produceTv(a);
}
Copy the code

Abstract TV product class that provides a show method.

public abstract class Television {
    public abstract void show(a);
}
Copy the code

Specific TV products Xiaomi TV:

public class XiaomiTelevision extends Television{
    @Override
    public void show(a) {
        System.out.println("Watch video on xiaomi TV"); }}Copy the code

Specific factory: Used to produce Millet TV:

public class XiaomiTvFactory extends TvFactory{
    @Override
    public Television produceTv(a) {
        System.out.println("Made a Xiaomi TV.");
        return newXiaomiTelevision(); }}Copy the code

Finally, write a test method:

public class Main {
    public static void main(String[] args) {
        Television XiaomiTelevision = newXiaomiTvFactory().produceTv(); XiaomiTelevision.show(); }}Copy the code

At this time, there is another demand for Huawei TV. Just like the production of Xiaomi TV, we need to generate specific factories and specific classes of Huawei TV, as follows:

public class HuaweiTvFactory extends TvFactory{
    @Override
    public Television produceTv(a) {
        System.out.println("Made a Huawei TV.");
        return newHuaweiTelevision(); }}Copy the code
public class HuaweiTelevision extends Television{
    @Override
    public void show(a) {
        System.out.println("Watch video on Huawei TV"); }}Copy the code

Then it’s ready to use

public class Main {
    public static void main(String[] args) {
        Television XiaomiTelevision = new XiaomiTvFactory().produceTv();
        XiaomiTelevision.show();
        Television HuaweiTelevision = newHuaweiTvFactory().produceTv(); HuaweiTelevision.show(); }}Copy the code

(4) Why use factory method mode

As you can see from the above example, the code becomes more complex using the factory method pattern and seems difficult to apply in a project. But the factory approach pattern brings something different

1. Good scalability

When designing a project, the first thing to do is to design the overall code architecture. Once you design the top-level abstract factory and the abstract product, all you need to do is add a concrete factory class that inherits the abstract factory when there are other underlying implementations.

2, good encapsulation

Once you have a factory class, it’s up to the factory class to implement the class. Assuming that the process of creating a class is complicated and has a lot of additional logic, this logic only needs to be implemented once in the factory class, and the specific products are directly available through the factory class. Reduce coupling between modules.

3. Isolation of framework and implementation

We can define abstract factories and abstract products as frameworks, and concrete factories and concrete products as implementations. With the factory method pattern, the framework and implementation are separated. Changes implemented at the bottom level do not result in changes at the top level.

(5) the application of factory method mode in the source code

Let’s look at how the factory method pattern is applied in SLF4J:

First, SLF4J defines an abstract factory, ILoggerFactory

public interface ILoggerFactory {
    Logger getLogger(String var1);
}
Copy the code

We then define an abstract product Logger:

public interface Logger {... . }Copy the code

There are many concrete factories that implement abstract factories. One SubstituteLoggerFactory is shown here

A specific factory is used to produce a specific product; SubstituteLogger is a specific product. As can be seen from the code above, the SubstituteLoggerFactory did some processing when producing a specific product. The beauty of the factory method is that when you actually use it, you only need to use the getLogger method to get the specific product.

(6) Summary

The factory method pattern is harder to understand and apply to a project than the adapter, iterator, or template method pattern. But we can still learn about the charm of design and the purpose of interfaces and abstract classes.