Learning is not so utilitarian, two brothers with you easily read source code ~

In the previous article, we learned how to use the Simple Factory model flexibly in Nacos. Design patterns also occur frequently in the interview process. When the interviewer asks: Tell me how the factory model works? What does he mean by the factory model?

In this extension to the extension, we’ll talk about the factory pattern again based on the simple factory pattern.

Factory method pattern

To answer the above questions, if we are talking about factory patterns in general terms, we usually include: simple factory/static factory, factory method pattern, and abstract factory pattern. While the simple factory pattern was described in the previous section, the abstract factory pattern is generally used in larger, more complex applications and is less common.

Therefore, if the factory pattern refers only to a pattern, then this is the factory method pattern.

An introduction to the factory method pattern

Factory Method patterns are also known as Factory patterns, polymorphic Factory patterns, and virtual constructor patterns.

When we talked about the simple factory pattern, we already knew that the pattern had some disadvantages, such as the factory-class business logic being too complex and violating the open closed principle and the principle of high cohesion and low coupling. Also, because methods are static, there is no way to form an inheritance hierarchy.

So, can we not abstract the factory class to provide a common interface, while the factory subclasses are responsible for creating concrete product objects, thus achieving the goal of decoupling?

Therefore, in the factory method pattern, the factory parent class defines the public interface for creating the product object, and the factory subclass is responsible for generating the concrete product object, using the factory subclass to determine which concrete product class should be instantiated.

Factory method pattern structure diagram

Before looking at the structure of the factory method pattern, let’s review the structure of the simple factory pattern:

We extend this structure diagram by abstracting the Factory class Factory, and we get the Factory method pattern:

The dotted lines in the figure above are the changes. The first change is that the factory class is abstracted, providing an abstract AbstractFactory. Now that we’re abstract, there must be a concrete factory implementation class that contrasts with the product. In this way, it is possible to modify the creation logic of one product without affecting the creation of other products, achieving the effect of decoupling.

Here’s a look at the core roles of the factory method pattern:

  • Abstract Product: An abstract class or interface for a Product that defines the characteristics and commonalities of the Product.
  • Concrete Product: Specific Product category.
  • AbstractFactory: The creation class of a product, the factory interface.
  • ConcreteFactory: Concrete creation class, factory implementation.

Factory method pattern implementation

We also use the Nacos configuration service and naming service as examples. Assume that both the configuration service and the naming service are integrated from the same NacosService, and that their creation is done through different sub-factories.

Define the abstract product class and its implementation class:

Public interface NacosService {/** * register(object object); public interface NacosService {/** * register(object object); } public class implements NacosService {@override public void register(Object Object) { System.out.println(" Configuration center instance registered successfully "); Public class NamingService implements NacosService {@override public void register(Object Object) { System.out.println(" Registered naming service successfully "); }}Copy the code

Define an abstract factory class and its implementation class:

Public interface NacosFactory {NacosService getService(); } public class ConfigNacosFactory implements NacosFactory {@override public implements NacosService getService() { return new ConfigService(); }} public class NamingNacosFactory implements NacosFactory {@override public NacosService implements NacosFactory () {  return new NamingService(); }}Copy the code

Client implementation:

Public class Client {public static void main(String[] args) {ConfigNacosFactory ConfigNacosFactory = new ConfigNacosFactory(); NacosService configService = configNacosFactory.getService(); configService.register(new Object()); NamingNacosFactory = new NamingNacosFactory(); NacosService namingService = namingNacosFactory.getService(); namingService.register(new Object()); }}Copy the code

The client can create different engineering subclasses as needed, and then implement specific product creation through specific factory subclasses. For the implementation of the factory method pattern, it is recommended to refer to the simple factory pattern, so that it is more convenient to understand and learn.

Advantages and disadvantages of the factory method pattern

The factory method pattern is a further abstraction of the simple factory pattern. The advantage is that it allows the system to introduce new products without modifying the original code, which satisfies the open closed principle.

Advantages:

  • The client only needs to know the specific factory to get the desired product, without knowing the specific creation process of the product;
  • Increased flexibility, for the creation of new products, just write a corresponding factory class;
  • Conforms to the open closed principle, conforms to the single responsibility principle, solves the class simple factory pattern cannot inherit the hierarchical structure problem.

Disadvantages:

  • The number of classes increases, and the corresponding factory implementation classes need to be added when adding new products;
  • Increasing the abstractness and difficulty of understanding the system;
  • Abstract products can only produce one product, if another product to change, still need to modify the product class. This disadvantage can be addressed using the abstract factory pattern.

Application scenarios of the factory method pattern

Application scenario:

  • The customer only knows the name of the factory that created the product, but does not know the specific product name;
  • The task of creating an object is done by one of several concrete factories, whereas an abstract factory only provides an interface to create a product.
  • The customer does not care about the details of creating the product, only about the brand;

summary

The factory pattern itself is not difficult, but one of the biggest headaches we have when learning design patterns is forgetting them. It’s easier to remember a design pattern when you have a deep understanding of why it was designed, how it was used, and how it evolved. The focus of this article is to conduct this kind of thinking guidance, have you learned?

If you have any questions or want to discuss the content of the article, please contact me (wechat: Zhuan2quan, remarks Nacos), if you think the writing is good, worth learning together, then pay attention to it.

Author of SpringBoot Tech Insider, loves to delve into technology and write about it.

Official account: “Program New Horizon”, the official account of the blogger, welcome to follow ~

Technical exchange: please contact the blogger wechat id: Zhuan2quan