JAVA Design Pattern-Factory Method (Factory Method)

Factory Method

The difference from the simple factory mode is that the factory method also abstracts the factory class into an interface (abstract class), and each product has its own corresponding factory class.

I talked about the simple factory model earlier. Now imagine how we need to change the code if we release a new product iphone13.

1) First, we have to create a new mobile phone product class Iphone13 to implement the Apple interface

2) Modify the code in the factory class and add the logic code of the new mobile phone.

The code I cited here is very simple, but in actual projects it has a very complex (deep-level) structure. Therefore, modifying the code in the factory class will be very cumbersome.

At this time the factory model is here! It's coming! Define the factory class as an interface, implement the Apple interface every time a new product is added, and create a corresponding factory class to implement the factory interface. This design can be extended without modifying the original code. Beautiful.

/**
 * 苹果系列
 */
public interface Apple {
    
    
}
public class Iphone12 implements Apple {
    
    
}
public class Iphone11 implements Apple {
    
    
}
public class Iphone10 implements Apple {
    
    
}

Factory class:

/**
 * 工厂类
 */
public interface AppleFactory {
    
    
    public Apple createPhone();
}
public class Iphone12Factory implements AppleFactory {
    
    
 	public Apple createPhone(){
    
    
        return new Iphone12();
    }   
}
public class Iphone11Factory implements AppleFactory {
    
    
	public Apple createPhone(){
    
    
        return new Iphone11();
    }   
}
public class Iphone10Factory implements AppleFactory {
    
    
    public Apple createPhone(){
    
    
            return new Iphone10();
     }   
}

Customer class:

public class Consumer {
    
    

    public static void main(String[] args) {
    
    
        Factory iphone12Factory = new Iphone12Factory();
        Apple iphone12 = iphone12Factory.createPhone();
        //这里我们需要什么产品类,用对应工厂类的createPhone()方法即可
        Factory iphone11Factory = new Iphone11Factory();
        Apple iphone11 = iphone11Factory.createPhone();
    }
}

Let me explain first, not which design pattern is necessarily the best. Which mode to use depends on the actual project.

Although the factory model is highly extensible, it is not necessary to modify the source code for new product categories, but when there are many product categories, a large number of factory categories will appear. We can consider the combination of simple factory pattern and factory method pattern.

Expand (a better factory model)

After reading the factory model above, let's look at another one. After reading this, I believe your understanding of the factory model will be closer.

Product category: Same as above (omitted)

Factory class:

/**
 * 工厂类
 */
public abstract AppleFactory {
    
    
    public abstract Apple createPhone();
    public void doSomething(){
    
    
        Apple iphone = createPhone();
        //I have to do something
    }
}
public class Iphone12Factory implements AppleFactory {
    
    
 	public Apple createPhone(){
    
    
        return new Iphone12();
    }   
}
public class Iphone11Factory implements AppleFactory {
    
    
	public Apple createPhone(){
    
    
        return new Iphone11();
    }   
}
public class Iphone10Factory implements AppleFactory {
    
    
    public Apple createPhone(){
    
    
            return new Iphone10();
     }   
}

Customer category:

public class Consumer {
    
    

    public static void main(String[] args) {
    
    
        Factory iphone12Factory = new Iphone12Factory();
        iphone12Factory.doSomething();
    }
}

It can be seen that the factory pattern is a design idea and does not limit whether your factory parent class is an interface or an abstract class. It only cares about how you achieve it (the factory model is that each product corresponds to a factory) .

In fact, there is not much difference between these two implementations. The latter does not directly return a product object, but uses this object to do something. Let me give you a chestnut. I give it to others as soon as I get the phone, and the others don't care. At this time doSomething() is to give to others.

Guess you like

Origin blog.csdn.net/weixin_43957211/article/details/110662285