This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

1. Introduction

For the factory pattern, there are three related design patterns, all of which are creative design patterns.

  1. Simple Factory pattern: In the simple Factory pattern, the instantiated operations are placed in a separate factory class, which becomes the simple factory class, and the simple factory class decides which concrete subclasses should be instantiated.
  2. Factory method: Defines an interface for creating objects, but it is up to subclasses to decide which class to instantiate. Factory methods defer instantiation to subclasses.
  3. Abstract Factory: The abstract factory pattern creates a family of objects, that is, many objects instead of one, and they are related, that is, they must be created together.


2 Simple factory mode

2.1 class diagram

As shown in the figure below, in SimpleFactory mode, the instantiation operation is put into SimpleFactory alone, which becomes a SimpleFactory class and lets the SimpleFactory class decide which concrete subclass should be instantiated.

Doing so decouples the implementation of the client class from the concrete subclass; the client class no longer needs to know which subclasses exist and which should be instantiated.


2.2 Implementation Mode

public interface IProduct {}Copy the code
public class Product1 implements Product {}Copy the code
public class Product2 implements Product {}Copy the code

Here is a simple factory class that instantiates the object

public class SimpleFactory {
    public Product createProduct(String type) {
        if ("Refrigerator".equal(type)) {
            return new Product1();
        }
        return newProduct2(); }}Copy the code

Here is how the Client is called

public class Client {
    public static void main(String[] args) {
        SimpleFactory simpleFactory = new SimpleFactory();
        Product product = simpleFactory.createProduct("Refrigerator"); }}Copy the code


2.3 Summary of simple factory model

Disadvantages of the simple factory pattern:

  1. Because the factory class centralizes all product creation logic, if it doesn’t work properly, the entire system suffers.
  2. Using the simple factory pattern will increase the number of classes in the system, increasing the complexity and understanding of the system at some point.
  3. The system is difficult to expand, and the factory logic has to be modified once new products are added, which also breaks the “on/off principle”.
  4. The simple factory pattern uses a static factory approach that prevents the factory roles from forming an inheritance-based hierarchy.

This design pattern can only be used if the factory is responsible for creating a small number of objects, and the simple factory pattern violates the open close principle by modifying the original code when adding new classes.


3. Factory method mode

3.1 class diagram

As shown in the figure below, a method to create a product is abstracted from Factory, but there is no implementation logic, leaving it to its subclasses to implement.


3.2 implementation

Suppose there are three Product objects to be created, Product, Product1, and Product2. Create three factory classes to implement the specific logic of creation. The advantage of this is that when a new Product is added, you only need to add a new factory class.

public abstract class Factory {
    abstract public Product factoryMethod(a);
    public void doSomething(a) { Product product = factoryMethod(); }}Copy the code
public class ConcreteFactory extends Factory {
    public Product factoryMethod(a) {
        return newProduct(); }}Copy the code
public class ConcreteFactory1 extends Factory {
    public Product factoryMethod(a) {
        return newProduct1(); }}Copy the code
public class ConcreteFactory2 extends Factory {
    public Product factoryMethod(a) {
        return newProduct2(); }}Copy the code


3.3 Summary of simple factory model

The factory method pattern is a good example of the dependency inversion principle: you cannot make high-level components dependent on low-level components, and both high-level and low-level components should depend on abstractions.

The disadvantage of the factory method is that every time a product is added, a concrete class and object implementation factory need to be added, which makes the number of classes in the system multiply, increasing the complexity of the system to a certain extent, but also increasing the dependence of the system concrete class.


4. Abstract factory pattern

4.1 class diagram

AbstractFactory mode uses factory method mode to create a single object. AbstractFactory createProductA() and createProductB() methods are implemented by subclasses. This conforms to the definition of the factory method pattern.

The concept of creating a family of objects is embodied in the Client. The Client uses AbstractFactory to call two methods at the same time to create two objects. In this case, the two objects are highly correlated and the Client needs to create both objects at the same time.

At a high level, abstract factories use composition, i.e. Cilent combines AbstractFactory, and factory method patterns use inheritance.


4.2 implementation

public class AbstractProductA {}Copy the code
public class AbstractProductB {}Copy the code
public class ProductA extends AbstractProductA {}Copy the code
public class ProductB extends AbstractProductB {}Copy the code
public abstract class AbstractFactory {
    abstract AbstractProductA createProductA(a);
    abstract AbstractProductB createProductB(a);
}
Copy the code
public class ConcreteFactory extends AbstractFactory {
    AbstractProductA createProductA(a) {
        return new ProductA();
    }

    AbstractProductB createProductB(a) {
        return newProductB(); }}Copy the code

In the Client, two objects are created simultaneously using the ConcreteFactory.

public class Client {
    public static void main(String[] args) {
        AbstractFactory abstractFactory = newConcreteFactory(); AbstractProductA productA = abstractFactory.createProductA(); AbstractProductB productB = abstractFactory.createProductB(); }}Copy the code


4.3 Abstract factory summary

The key point of the abstract factory pattern implementation is to define the factory interface and product interface, but how to implement the factory and product itself needs to be left to the concrete subclass implementation, the client only with the abstract factory and abstract product