[Design Patterns] Creative Patterns - Factory Method Patterns

Creative Pattern - Factory Method Pattern

1. Definition

The factory method pattern is a creational design pattern that provides a method of creating an object in the parent class , allowing the subclass to determine the type of instantiated object.

Second, the problem

Assuming that a company's express transportation is only transported on land by trucks at first, and later, with the increase in the number of people and demand, it needs to be shipped overseas, so to increase the number of ships to be transported by sea, then we need to make extensive changes to the code of the program. , which obviously does not conform to the open-closed principle.

3. Solutions

The factory method pattern is a good solution to this problem. Use special factory methods instead of direct calls to object constructors (new object).

4. Realization

1. The product
declares the interface. These interfaces are common to all objects constructed by the creator and its subclasses

package com.atmae.factory;

/**
 * @Author: Mae
 * @Date: 2022/4/6
 * @Time: 19:49
 * @Description:
 */
public interface Product {
    
    
    void deliver();
}

2. A specific product
is a different realization of the product interface.
truck:

package com.atmae.factory;

/**
 * @Author: Mae
 * @Date: 2022/4/6
 * @Time: 19:49
 * @Description:
 */
public class Truck implements Product{
    
    
    @Override
    public void deliver() {
    
    
        System.out.println("使用卡车在陆地运输");
    }
}

Ship:

package com.atmae.factory;

/**
 * @Author: Mae
 * @Date: 2022/4/6
 * @Time: 19:48
 * @Description:
 */
public class Ship implements Product{
    
    
    @Override
    public void deliver() {
    
    
        System.out.println("使用轮船在海上运输");
    }
}

3. Create a factory
class to declare a factory method that returns a product object. The method's return object type must match the product interface.

package com.atmae.factory;

/**
 * @Author: Mae
 * @Date: 2022/4/6
 * @Time: 19:47
 * @Description:
 */
public abstract class Factory {
    
    
  public abstract Product createProduct();
}

4. Specific factory class

The base factory method will be overridden to return a different type of product.
package com.atmae.factory;

/**
 * @Author: Mae
 * @Date: 2022/4/6
 * @Time: 19:48
 * @Description:
 */
public class ShipFactory extends Factory{
    
    

    @Override
    public Product createProduct() {
    
    
        return new Ship();
    }
}
package com.atmae.factory;

/**
 * @Author: Mae
 * @Date: 2022/4/6
 * @Time: 19:48
 * @Description:
 */
public class TruckFactory extends Factory{
    
    
    @Override
    public Product createProduct() {
    
    
        return new Truck();
    }
}

5. Client

The client only needs to use the abstract class to create the factory to implement the concrete factory (polymorphism), and the concrete factory returns the product type

package com.atmae.factory;

/**
 * @Author: Mae
 * @Date: 2022/4/6
 * @Time: 19:48
 * @Description:
 */
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Factory factory=new ShipFactory();
        Product product = factory.createProduct();
        product.deliver();
    }
}

Five, UML diagram

insert image description here

Six, the factory method pattern is suitable for the scene

  • In the process of writing code, if the exact class of objects and their dependencies cannot be predicted, factory methods can be used.
  • If you want to reuse existing objects to save system resources, instead of recreating the object every time, you can use the factory method.
  • Want users to extend the internal components of your software library or framework, you can use factory methods

7. Summary

advantage

  • Single Responsibility Principle. You can place product creation code in a single location in your program, making the code easier to maintain.
  • Open closed principle. You can introduce new product types into your program without changing existing client code.
  • Tight coupling between factories and concrete products can be avoided.

shortcoming

  • Introduces many new subclasses, the code may become more complex as a result

8. Comparison with other modes

  • With Simple Factory
    Simple Factory is where all products are managed by one factory, while Factory Method is where each product is managed by different factories, all of which inherit from abstract factory class.
    Factory methods move the internal logic of a simple factory to the client. Adding functionality to modify the client is no longer the factory class.

Guess you like

Origin blog.csdn.net/weixin_51799151/article/details/123997846