The factory pattern is a creation pattern.

Definition of the factory pattern

Factory mode includes simple factory, factory method and abstract factory. This article covers the first two.

  • Purpose: Loose coupling, not specific to specific classes.
  • What it does: Reduces the application’s dependency on concrete classes, enabling loose coupling. Programming for abstractions, not concrete classes.

There are two types of factory methods: simple factory and factory method patterns. This article will explain the abstract factory pattern.

Abstract Factory pattern

Provides an interface for creating a set of related or interdependent objects without specifying their concrete classes.

Provides an interface for creating families of related or dependent objects without explicitly specifying concrete classes. Allowing customers to use abstract interfaces to create a set of related products without knowing what is produced, customers are decoupled from concrete products.

composition

The class diagram for the abstract factory pattern looks like this:

Contains the following roles:

  • Abstract products: Abstract products are abstractions from (many) different products. Product can be an interface or abstract class.
  • ConcreteProduct: the product object returned in the factory is actually created through ConcreteProduct.
  • Factory entity: provides external interfaces. A client or other program obtains a Product object through the Factory interface.
  • Abstract factory: Used to create families of related or dependent objects without explicitly specifying concrete classes.

Implementation of the abstract factory pattern

Product interfaces

public interface ProductBI { 
  public void productName(); 
} 
public interface ProductAI { 
  public void productName(); 
}Copy the code

Product Entity Class

public class ProductA1 implements ProductAI { @Override public void productName() { System.out.println("product A1"); }}...Copy the code

The abstract factory

public interface AbstractFactory { 
  public ProductAI createProductA(); 
  public ProductBI createProductB(); 
}
Copy the code

Factory Entity Class

public class Factory1 implements AbstractFactory { 
  @Override 
  public ProductAI createProductA() { 
      return new ProductA1(); 
  } 
  @Override 
  public ProductBI createProductB() { 
      return new ProductB1(); 
  } 
}
public class Factory2 implements AbstractFactory { 
  @Override 
  public ProductAI createProductA() { 
      return new ProductA2(); 
  } 
  @Override 
  public ProductBI createProductB() { 
      return new ProductB2(); 
  } 
}Copy the code

conclusion

Abstract Factory Each method is often implemented as a factory method whose task is to define an interface responsible for creating a set of products. Each method in this interface is responsible for creating a concrete product and provides concrete practices using the implementation abstract factory subclass. The advantage is that you can combine related products, while the disadvantage is that extensions require interface modifications.

Factory method pattern vs. Abstract factory pattern

  • Abstract factory is the key to the abstract relationship between products, so at least two products; The factory approach is to generate products without focusing on the relationships between products, so you can only generate one product.
  • In the abstract factory, the client makes the abstract relationship of the product clear. In the final use, the client (and its interface) is generally used, and the relationship between the products is encapsulated and fixed. The factory approach uses the product itself (and its interfaces) for final use.
  • Abstract factory factories are classes; The factory of the factory method is the method.

The abstract factory is more like a complex version of the policy pattern, which changes the treatment or result by changing the policy; The client of the abstract factory also changes the results by changing the factory. So when you use it, you use the client and change factories, and you don’t see the product itself. The purpose of the factory method is to produce the product, so the product can be seen and used. Of course, if the product is used within the creator, the factory approach is intended to perfect the creator so that the creator can be used. In addition, the creator himself cannot replace the product produced.

The factory class of an abstract factory does one thing and produces a product. Produce products for the client to use, never for their own use. The factory method produces products that can be used by the system, by the client, or by its own class. Your own class can have other functional methods besides the factory method

reference

  1. Design patterns three factory patterns
  2. What is the difference between abstract factory pattern and factory pattern?