Creational Pattern - Difference Between Factory Method Pattern and Abstract Factory Pattern

The factory method pattern belongs to the object creation pattern, which defines an interface for users to create objects, and lets subclasses decide which class to instantiate. The factory method pattern defers the instantiation of a class to its subclasses.

Specifically, it is an abstract product class that derives multiple concrete product classes; at the same time, an abstract factory class derives multiple concrete factory classes. And each specific factory class can only create an instance of a specific product class.

The abstract factory pattern also belongs to the object creation pattern, which provides an interface to create a series of related or interdependent objects without specifying their specific classes.

Specifically, in multiple abstract product classes, each abstract product class can derive multiple concrete product classes. An abstract factory class can derive multiple concrete factory classes. Each specific factory class can create multiple instances of specific product classes

Difference: The factory method pattern has only one abstract product class, while the abstract factory pattern has multiple. The concrete factory class of the factory method pattern can only create one instance of the concrete product class, while the abstract factory pattern can create multiple instances.

Factory method: To put it bluntly, it is a method. This method is to create a specific product. It requires all factories to have a method with the same signature, and rewrite the method if necessary; Abstract factory: You cannot directly create products, only factories can be created , that is, the products created by the abstract factory are factories. Although it also defines the method of creating products, it needs to create a specific factory implementation, that is: the factory created by the abstract factory creates products.

The factory method adopts the class inheritance mechanism (generate a subclass, rewrite the factory method, and produce an object in this method); the abstract factory adopts the object combination mechanism, and specifically defines the "factory" object to be responsible for the creation of the object . The way objects are composed is by passing "factory" objects as parameters.

For example, factories can produce mice and keyboards. Then the objects of the implementation class of the abstract factory (a specific subclass of it) can produce mice and keyboards, but factory A may produce Logitech keyboards and mice, and factory B is Microsoft's. 

In this way, A and B are factories, corresponding to abstract factories; 

The mouse and keyboard produced by each factory are products, corresponding to the factory method; 

Using the factory method pattern, you can change the keyboard from Logitech to Microsoft by replacing the factory method that generates the keyboard. But with the abstract factory pattern, you can replace the mouse and keyboard at the same time as long as you change the factory. If you want dozens of products, of course it is most convenient to replace them all at once with the abstract factory pattern (this factory will use the corresponding factory method for you) 

So the abstract factory is like a factory, and the factory method is like a product line of the factory

Guess you like

Origin blog.csdn.net/rukawashan/article/details/124485882