Factory pattern-simple factory, factory method, abstract factory analysis

Today, I will learn a design pattern that is very widely used and very powerful, the factory pattern. The main core is to help us program abstractly, rather than specific types of programming.

Starting from the problem

  1. What is a simple factory? Is it a design pattern? If you use it?
  2. What is the factory method? What is abstract?
  3. What is an abstract factory? What is abstract?
  4. The difference between factory method and abstract factory?

Simple factory pattern

Simple factory is not considered a real design pattern, but more like a programming habit of ours, but it is a simple way to decouple client programs from specific classes in usual coding.

Introduction

The factory class has a factory method (create) that accepts a parameter and instantiates different product classes through different parameters.

Icon:

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-X7n1n11W-1597520420651)(en-resource://database/2726:1)]

Pros and cons

  • advantage:
  1. Obviously, the characteristic of the simple factory is "simple and rude". Through a factory method containing parameters, we can instantiate any product category, from airplanes and rockets to potato noodles.
  2. So the simple factory has an alias: God class.
  • Disadvantages:
  1. Any subcategory of "things" can be produced, which is too heavy. When there are many types of products to be produced, the amount of code in the factory method may be huge.
  2. Under the condition of following the opening and closing principle (open for expansion, closed for modification), Simple Factory is powerless to add new products. Because adding new products can only be achieved by modifying the factory method.

The factory method can just solve these two shortcomings of the simple factory. Then look down. Through this kind of progressive learning, you will definitely understand it thoroughly.

Tips: Spring solves the shortcomings of simple factories through configuration files and reflection.

how to use

public class AnimalFactory {
    
    
    //简单工厂设计模式(负担太重、不符合开闭原则)
    public static Animal createAnimal(String name) {
    
    
        if ("cat".equals(name)) {
    
    
            return new Cat();
        } else if ("dog".equals(name)) {
    
    
            return new Dog();
        } else if ("cow".equals(name)) {
    
    
            return new Dog();
        } else {
    
    
            return null;
        }
    }
}

Factory method

An interface for creating objects is defined, but the subclass determines which class to instantiate. The factory method defers the instantiation of the class to the subclass.

Icon

Insert picture description here

Pros and cons

  • advantage
  1. The factory method pattern is a good way to reduce the burden of the factory class, and hand over a certain type/a thing to a factory for production; (corresponding to the shortcomings of the simple factory 1.
  2. At the same time, adding a certain type of "thing" does not need to modify the factory class, just add a factory that produces such "thing", so that the factory class conforms to the open-closed principle.
  • Disadvantage
  1. It is more complicated to deal with certain situations that can form a product family (a group of products).

Example

// 抽象出来的动物工厂----它只负责生产一种产品
public abstract class AnimalFactory {
    
    
    // 工厂方法
    public abstract Animal createAnimal();
}

// 具体的工厂实现类 
public class CatFactory extends AnimalFactory {
    
     
    @Override public Animal createAnimal() {
    
     
        return new Cat(); 
    } 
}

//具体的工厂实现类 
public class DogFactory extends AnimalFactory {
    
     
    @Override public Animal createAnimal() {
    
     
        return new Dog(); 
    } 
}

Abstract factory pattern

Provide an interface for creating families of related or dependent objects without the need to specify specific classes.

For example, cars can be divided into cars, SUVs, MPVs, etc., as well as Mercedes-Benz, BMW, etc. We can regard all Mercedes-Benz cars as one product family, and all BMW cars as another product family. Corresponding to two factories, one is a Mercedes-Benz factory, and the other is a BMW factory. Different from the factory method, Mercedes-Benz's factory does not only produce a specific product, but a family of products (Mercedes-Benz sedan, Mercedes-Benz SUV, Mercedes-Benz MPV). The "abstraction" of "abstract factory" refers to this. That is, compared with the factory method, the abstract factory defines a series of products, rather than a product.

The above factory method pattern is an extreme abstract factory pattern (that is, an abstract factory pattern that only produces one product), and the abstract factory pattern can be regarded as a promotion of the factory method pattern.

Icon

Insert picture description here

Factory mode difference

  • Simple factory: Use a factory object to produce any product in the same hierarchical structure. (Does not support expansion to increase products)
  • Factory method: Use multiple factory objects to produce corresponding fixed products in the same hierarchical structure. (Support to expand and increase products)
  • Abstract factory: Use multiple factory objects to produce all products of different product families. (Does not support expanding and adding products; supporting adding product families)

OO principles newly learned today: rely on abstraction, don’t rely on concrete classes

Guess you like

Origin blog.csdn.net/taurus_7c/article/details/107295308