Simple factory pattern, factory pattern, abstract factory pattern

The factory, as its name suggests, is to create products. According to whether the product is a specific product or a specific factory, it can be divided into simple factory mode and factory method mode, and according to the degree of abstraction of the factory, it can be divided into factory method mode and abstract factory mode. This mode is used to encapsulate and manage the creation of objects and is a creational mode.
table of Contents

Simple factory pattern
Let’s talk about the simple factory pattern first. The simple factory method contains a business logic that selects the initial class from the class hierarchy. The
customer class does not directly create the product class. The customer class is only the consumer of the object. The simple factory pattern realizes separation of responsibilities. The client class is not responsible for creating objects of the class.
UML diagram:
Insert picture description here
Give an example to better understand these three modes, the example of selling milk.
Milk types: Mengniu milk and Yili milk.

public interface Mike {
    
    
    public String make();
}
public class MengNiuMilk implements Mike{
    
    
    @Override
    public String make() {
    
    
        return "制作蒙牛牛奶";
    }
}
public class YiLiMilk implements Mike {
    
    
    @Override
    public String make() {
    
    
        return "制作伊利牛奶";
    }
}

public class MikeFactory {
    
    
    public Mike getMike(String mikeName){
    
    
        if("蒙牛牛奶".equals(mikeName)){
    
    
            return new MengNiuMilk();
        }
        if("伊利牛奶".equals(mikeName)){
    
    
            return new YiLiMilk();
        }
        return null;
    }
}
//测试
public class Test {
    
    
    public static void main(String[] args) {
    
    
        MikeFactory mikeFactory = new MikeFactory();
        String name = mikeFactory.getMike("蒙牛牛奶").make();
        System.out.println(name);
        System.out.println(mikeFactory.getMike("伊利牛奶").make());
    }
}

Output:

制作蒙牛牛奶
制作伊利牛奶

Factory pattern
In the factory class of the simple factory pattern, you must know how to create objects of each subclass, so whenever a product class is added, or the code of the factory class needs to be modified, it does not conform to the principle of opening and closing. The reason that this pattern does not conform to the principle of opening and closing is that the factory method is just an entity class. Whenever a new product is added, the conditional statement must be modified in the factory class. Therefore, the factory model is introduced, an interface is used as a superclass, and the class hierarchy of the same product class produces a creator class structure, and each is responsible for creating the corresponding product.
UML diagram:
Insert picture description here
This example is the same as above, but the AbstractFactory interface needs to be added, and different product factories implement this interface.
Types of factories: Yili factory, Mengniu factory.

public interface AbstractFactory {
    
    
    public Mike mikeMake();
}
public class YiLiFactory implements AbstractFactory {
    
    
    @Override
    public Mike mikeMake() {
    
    
        return new YiLiMilk();
    }
}
public class MengNiuFactory implements AbstractFactory {
    
    
    @Override
    public Mike mikeMake() {
    
    
        return new MengNiuMilk();
    }
}
//测试
public class Test {
    
    
    public static void main(String[] args) {
    
    
        YiLiFactory yiLiFactory = new YiLiFactory();
        System.out.println(yiLiFactory.mikeMake().make());
        MengNiuFactory mengNiuFactory = new MengNiuFactory();
        System.out.println(mengNiuFactory.mikeMake().make());
    }
}

Output:

制作伊利牛奶
制作蒙牛牛奶

Abstract Factory Model
No matter how the factory is divided and abstracted, the above two models are only for one type of product milkMilk. If you want to produce another product milk powderPowderedMilk, how should it be expressed?
The easiest way is to make a complete copy of the factory method model introduced in 2, but this time milk powder is produced. In other words, we have to completely copy and modifyall codes of milk production management, which is not conducive to expansion and maintenance.
The abstract factory modelAbstarctFactoryadds an interface for creating products in it, and realizes the creation of new products in concrete sub-factories.
UML diagram:
Insert picture description here
Before facing the same type of product, now add another type of product: milk powder.
New types of milk powder: Yili milk powder, Mongolian milk powder.

public interface PowderedMilk {
    
    
    public String make();
}
public class MengNiuPowderedMilk implements PowderedMilk {
    
    
    @Override
    public String make() {
    
    
        return "蒙牛奶粉";
    }
}
public class YiLiPowderedMilk implements PowderedMilk {
    
    
    @Override
    public String make() {
    
    
        return "伊利奶粉";
    }
}
public class MengNiuFactory implements AbstractFactory {
    
    
    @Override
    public Mike mikeMake() {
    
    
        return new MengNiuMilk();
    }

    @Override
    public PowderedMilk powderedMilkMake() {
    
    
        return new MengNiuPowderedMilk();
    }
}
public class YiLiFactory implements AbstractFactory {
    
    
    @Override
    public Mike mikeMake() {
    
    
        return new YiLiMilk();
    }

    @Override
    public PowderedMilk powderedMilkMake() {
    
    
        return new YiLiPowderedMilk();
    }
}
//测试
public class Test {
    
    
    public static void main(String[] args) {
    
    
        YiLiFactory yiLiFactory = new YiLiFactory();
        System.out.println(yiLiFactory.mikeMake().make());
        System.out.println(yiLiFactory.powderedMilkMake().make());
        MengNiuFactory mengNiuFactory = new MengNiuFactory();
        System.out.println(mengNiuFactory.mikeMake().make());
        System.out.println(mengNiuFactory.powderedMilkMake().make());
    }
}

Output:

制作伊利牛奶
伊利奶粉
制作蒙牛牛奶
蒙牛奶粉

Guess you like

Origin blog.csdn.net/weixin_43213064/article/details/112239091