Learn not so utilitarian, two brothers with you easily read the source code ~

In the previous article, we learned how Simple Factory is used flexibly in NaCos. Design patterns also come up frequently during the interview process. When the interviewer asks, “Tell me about the factory model. What does he mean by the factory model?

This article is an extension of the extension, based on the simple factory pattern, to talk about the factory pattern.

Factory method model

To answer the above question, if you talk about factory patterns in general, they usually include: simple factory/static factory, factory method pattern, and abstract factory pattern. While the simple factory pattern was covered 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 one pattern, it is the factory method pattern.

Introduction to Factory Method Patterns

Factory Method Pattern is also called Factory Pattern, Polymorphic Factory Pattern and Virtual Constructor Pattern.

When we talk about the simple factory pattern, we already know that it has some disadvantages, such as that the factory-type business logic is too complex, which violates the open closed principle and the principle of high cohesion and low coupling. Also, because the method is static, there is no hierarchy of inheritance.

Wouldn’t we achieve decoupling by abstracting the factory class, providing a common interface, and subclasses of the factory class being responsible for creating concrete product objects?

Thus, in the factory method pattern, the factory parent defines the public interface that creates the product object, and the factory subclass is responsible for generating the concrete product object that determines which concrete product class should be instantiated.

Factory Method Pattern Structure Diagram

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

We extend this structure by abstracting the Factory class, Factory, to get the Factory method pattern:

The dotted lines in the image above are the changes. The first change is that the factory class has been abstracted, providing an abstract AbstractFactory. Since this is abstract, there must be a concrete factory implementation class that is compared to the product. In this way, the creation logic of one product can be modified without affecting the creation of other products, and the effect of decoupling can be achieved.

Here’s a look at the core roles of the Factory Method pattern:

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

Factory method pattern implementation

We also use the configuration service and naming service of NaCos as examples here. Suppose that the configuration service and the naming service are both integrated from the same NacosService, and at the same time, they are created through different subfactories.

Define abstract product classes and their implementation classes:

Public void register() {public void register() {public void register(); public void register() {public void register(); } public class ConfigService implements NacosService {@Override public void register(Object Object) {// Override public void register(Object Object); System.out.println(" Configuration Center instance registered successfully "); }} public class NameService implements NacosService {@Override public void register(Object Object) {// Override public void register(Object Object) {// Override public void register(Object Object); System.out.println(" Naming service registered successfully "); }}

Define the abstract factory class and its implementation class:

Public interface nacosFactory {nacosService getService(); public interface nacosService getService(); } public class confignacosFactory implements NacosFactory {@Override public NacosService implements NacosFactory () {// Override public NacosService implements NacosFactory () { return new ConfigService(); Public class NacosFactory implements NacosFactory {@Override public NacosService implements NacosFactory () {public NacosService implements NacosFactory ();  return new NamingService(); }}

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()); }}

The client can create different engineering subclasses according to the needs, and then realize the specific product creation through the specific factory subclass. As for the implementation of the factory method pattern, it is recommended to refer to the simple factory pattern, which is easier to understand and learn.

Advantages and disadvantages of the factory approach pattern

The factory method pattern is a further abstraction of the simple factory pattern, which has the advantage of allowing the system to introduce new products without modifying the original code, that is, satisfying 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, only need to write a corresponding factory class;
  • In line with the open closed principle, in line with the principle of single responsibility, solve the class simple factory model can not inherit the hierarchical structure problem.

Disadvantages:

  • The number of classes increases, and the corresponding factory implementation classes need to be added when new products are added.
  • It increases the abstractness and difficulty of understanding the system.
  • An abstract product can only produce one product, and if you change a product, you still need to modify the product class. This drawback can be addressed using the abstract factory pattern.

Application scenarios for the Factory Method pattern

Application scenarios:

  • 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 performed by one of a number of concrete factories, while an abstract factory simply provides an interface to create a product.
  • Customers don’t care about the details of creating the product, they only care about the brand of the product;

summary

The factory pattern itself is not difficult, but the biggest headache we have when learning design patterns is forgetting them. If, like in this article, you have a deep understanding of why design patterns were designed, how they were used, and how they evolved, it will be easier to remember them. The point of this article is to guide your thinking in this area. Have you learned this?

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

Author of the technical book SpringBoot Inside Technology, loves to dig into technology and write good articles about technology.

Public number: “Program new horizon”, the public number of bloggers, welcome to pay attention to ~

Technical exchange: please contact the blogger WeChat ID: zhuan2quan