【Spring】Factory pattern

Based on the latest Spring framework tutorial of [Power Node], the first Spring 6 tutorial on the entire network, learn spring from scratch to advanced with Lao Du and Lao Du’s original notes https://www.yuque.com/docs/share/866abad4-7106 -45e7-afcd-245a733b073f?# "Spring6" is organized, document password: mg9b


Spring related articles are compiled and summarized at: https://www.yuque.com/u27599042/zuisie


  • Design pattern: a solution that can be reused
  • Development principles are the basis for software development. Design patterns will try their best to meet the development principles.
  • The GoF (Gang of Four) group of four described 23 design patterns in the book "Design Patterns: Elements of Reusable Object-Oriented Software" (i.e., "Design Patterns"), so generally speaking, GoF refers to 23 types Design Patterns
  • What we usually call design patterns refers to the 23 design patterns described in the book "Design Patterns"
  • In addition to the 23 design patterns of GoF, there are other design patterns, such as: JavaEE design patterns (DAO pattern, MVC pattern, etc.).
  • GoF23 design patterns can be divided into three categories:
    • Creation type (5): Solve the problem of object creation.
      • It includes singleton pattern, factory method pattern, abstract factory pattern, builder pattern, and prototype pattern.
    • Structural type (7): a classic structure in which some classes or objects are grouped together.
    • Behavioral type (11): Solve interaction problems between classes or objects.
  • The factory pattern solves the problem of object creation, so the factory pattern is a creational design pattern.
  • This is because the underlying Spring framework uses a large number of factory patterns.

Three forms of factory pattern

  • Simple Factory: Not one of the 23 design patterns.
    • The simple factory pattern is also called: static factory method pattern.
    • The simple factory pattern is a special implementation of the factory method pattern.
  • Factory Method: It is one of the 23 design patterns.
  • Abstract Factory pattern (Abstract Factory): It is one of the 23 design patterns.

Simple factory pattern

  • BeanFactory in Spring uses the simple factory pattern.
  • In the simple factory pattern, the method of producing products in the factory class is a static method
  • The role of the simple factory pattern includes three:
    • abstract product persona
    • specific product role
    • factory role

abstract product persona

/**
 * ClassName: Weapon
 * Package: simple.cw.spring
 * Description:
 * 抽象产品角色 - 武器抽象类
 *
 * @Author tcw
 * @Create 2023-05-23 20:52
 * @Version 1.0
 */
public abstract class Weapon {
    
    
    
    /**
     * 武器的攻击方法
     */
    public abstract void attack();
    
}

Specific product roles

/**
 * ClassName: Tank
 * Package: simple.cw.spring
 * Description:
 * 具体产品角色 - 坦克类
 *
 * @Author tcw
 * @Create 2023-05-23 20:55
 * @Version 1.0
 */
public class Tank extends Weapon{
    
    
    @Override
    public void attack() {
    
    
        System.out.println("开炮!!!!!!");
    }
}

/**
 * ClassName: Fighter
 * Package: simple.cw.spring
 * Description:
 * 具体产品角色 - 飞机类
 *
 * @Author tcw
 * @Create 2023-05-23 20:56
 * @Version 1.0
 */
public class Fighter extends Weapon {
    
    
    @Override
    public void attack() {
    
    
        System.out.println("投掷炸弹....");
    }
}

/**
 * ClassName: Dagger
 * Package: simple.cw.spring
 * Description:
 * 具体产品角色 - 匕首类
 *
 * @Author tcw
 * @Create 2023-05-23 20:56
 * @Version 1.0
 */
public class Dagger extends Weapon{
    
    
    @Override
    public void attack() {
    
    
        System.out.println("砍!砍!砍!砍!");
    }
}

factory role

  • Since the method of producing products in the factory class in the simple factory pattern is a static method, the simple factory pattern is also called the static factory method pattern.
/**
 * ClassName: WeaponFactory
 * Package: simple.cw.spring
 * Description:
 * 工厂类角色 - 武器工厂
 *
 * @Author tcw
 * @Create 2023-05-23 20:58
 * @Version 1.0
 */
public class WeaponFactory {
    
    
    
    /**
     * 根据需要的武器类型创建武器
     *
     * @param weaponType 需要的武器类型
     * @return 需要的武器类型相应的武器对象
     */
    public static Weapon getWeapon(String weaponType) {
    
    
        // 将要返回的武器对象
        Weapon weapon = null;
        // 根据需要的武器类型创建武器
        switch (weaponType) {
    
    
            case "tank":
                weapon = new Tank();
                break;
            case "fighter":
                weapon = new Fighter();
                break;
            case "dagger":
                weapon = new Dagger();
                break;
        }
        return weapon;
    }
}

client program

  • For the client, there is no need to know the specific production process of the product, just use the factory role to produce the product directly.
  • Since the client does not need to care about the specific production process of the product, the client only needs to be responsible for consumption, and the factory role only needs to produce the corresponding products according to the needs of the client. That is, the client is only responsible for consumption, and the factory role is only responsible for production, so that Producers and consumers are separated
  • The role of the simple factory pattern: Separate the responsibilities of producers and consumers, so that neither party needs to care about the specific details of the other party
@org.junit.Test
public void test01() {
    
    
    // 利用工厂角色生产产品(生产者生产产品)
    Weapon tank = WeaponFactory.getWeapon("tank");
    Weapon fighter = WeaponFactory.getWeapon("fighter");
    Weapon dagger = WeaponFactory.getWeapon("dagger");
    // 使用武器进行攻击(消费者消费产品)
    tank.attack();
    fighter.attack();
    dagger.attack();
}

image.png

The problem solved by the simple factory pattern (advantages)

  • The client program does not need to care about the details of object creation. When it needs an object, it only needs to ask the factory for it, which initially realizes the separation of responsibilities.
  • The client is only responsible for "consumption", and the factory is responsible for "production". Production and consumption are separated.

Disadvantages of the simple factory pattern

  • The simple factory mode does not comply with the OCP opening and closing principle, because when the system needs to be expanded, the originally written factory class needs to be modified.
  • The factory class has greater responsibilities and cannot cause any problems, because this factory class is responsible for the production of all products and is called the omnipotent class, or some people call it the God class. Once something goes wrong with this factory class, the entire system will inevitably be paralyzed. (Don’t put all your eggs in one basket)

factory method pattern

  • The difference between the factory method pattern and the simple factory pattern is that one product corresponds to a factory class
  • The roles of the factory method pattern include:
    • abstract factory role
    • Specific Factory Roles
    • abstract product persona
    • Specific product roles

abstract product persona

/**
 * ClassName: Weapon
 * Package: simple.cw.spring
 * Description:
 * 抽象产品角色 - 武器抽象类
 *
 * @Author tcw
 * @Create 2023-05-23 20:52
 * @Version 1.0
 */
public abstract class Weapon {
    
    
    
    /**
     * 武器的攻击方法
     */
    public abstract void attack();
    
}

Specific product roles

/**
 * ClassName: Tank
 * Package: simple.cw.spring
 * Description:
 * 具体产品角色 - 坦克类
 *
 * @Author tcw
 * @Create 2023-05-23 20:55
 * @Version 1.0
 */
public class Tank extends Weapon{
    
    
    @Override
    public void attack() {
    
    
        System.out.println("开炮!!!!!!");
    }
}

/**
 * ClassName: Fighter
 * Package: simple.cw.spring
 * Description:
 * 具体产品角色 - 飞机类
 *
 * @Author tcw
 * @Create 2023-05-23 20:56
 * @Version 1.0
 */
public class Fighter extends Weapon {
    
    
    @Override
    public void attack() {
    
    
        System.out.println("投掷炸弹....");
    }
}

/**
 * ClassName: Dagger
 * Package: simple.cw.spring
 * Description:
 * 具体产品角色 - 匕首类
 *
 * @Author tcw
 * @Create 2023-05-23 20:56
 * @Version 1.0
 */
public class Dagger extends Weapon{
    
    
    @Override
    public void attack() {
    
    
        System.out.println("砍!砍!砍!砍!");
    }
}

abstract factory role

/**
 * ClassName: WeaponFactory
 * Package: simple.cw.spring
 * Description:
 * 抽象工厂角色 - 武器工厂
 *
 * @Author tcw
 * @Create 2023-05-23 20:58
 * @Version 1.0
 */
public abstract class WeaponFactory {
    
    
    
    /**
     * 生产武器
     *
     * @return 武器
     */
    public abstract Weapon getWeapon();
}

Specific Factory Roles

/**
 * ClassName: DaggerFactory
 * Package: simple.cw.spring
 * Description:
 * 具体工厂角色 - 生产匕首的工厂
 *
 * @Author tcw
 * @Create 2023-05-24 18:15
 * @Version 1.0
 */
public class DaggerFactory extends WeaponFactory {
    
    
    @Override
    public Weapon getWeapon() {
    
    
        return new Dagger();
    }
}

/**
 * ClassName: TankFactory
 * Package: simple.cw.spring
 * Description:
 * 具体工厂角色 - 生产坦克的工厂
 *
 * @Author tcw
 * @Create 2023-05-24 18:18
 * @Version 1.0
 */
public class TankFactory extends WeaponFactory{
    
    
    @Override
    public Weapon getWeapon() {
    
    
        return new Tank();
    }
}

/**
 * ClassName: FighterFactory
 * Package: simple.cw.spring
 * Description:
 * 具体工厂角色 - 生产飞机的工厂
 *
 * @Author tcw
 * @Create 2023-05-24 18:19
 * @Version 1.0
 */
public class FighterFactory extends WeaponFactory{
    
    
    @Override
    public Weapon getWeapon() {
    
    
        return new Fighter();
    }
}

client program

@org.junit.Test
public void test01() {
    
    
    // 创建工厂类对象
    WeaponFactory tankFactory = new TankFactory();
    WeaponFactory fighterFactory = new FighterFactory();
    WeaponFactory daggerFactory = new DaggerFactory();
    // 利用工厂对象生产产品
    Weapon tank = tankFactory.getWeapon();
    Weapon fighter = fighterFactory.getWeapon();
    Weapon dagger = daggerFactory.getWeapon();
    // 使用武器进行攻击
    tank.attack();
    fighter.attack();
    dagger.attack();
}

Problems Solved by the Factory Method Pattern (Advantages)

  • Factory method pattern, since a product corresponds to a factory class, so when adding a new product, you only need to add a factory class and a specific product class. There is no need to modify the original program. When the client needs a new product, it only needs to call The corresponding method of the new factory class can be used, which solves the problem that the simple factory model violates OCP principles;
    • High scalability
  • At the same time, since a product corresponds to a factory class, the factory is not an all-purpose class, which avoids the problem that once a problem occurs in the factory class, the entire system will inevitably be paralyzed.
  • When a caller wants to create an object, he only needs to know its name, and production and consumption are separated.
  • Shield the specific implementation of the product, the caller only cares about the product interface.

Disadvantages of the factory method pattern

  • Every time a product is added, a concrete class and object implementation factory need to be added, doubling the number of classes in the system, which increases the complexity of the system to a certain extent and also increases the dependence on the system's concrete classes.
  • Class explosion. As the number of classes increases sharply, the complexity of the relationships between classes also increases sharply, making the system difficult to maintain.
  • To solve the shortcomings of the factory method pattern, use the abstract factory pattern

Guess you like

Origin blog.csdn.net/m0_53022813/article/details/132058435