Source: ThinkKeep’s Design-Patterns project
Author: JasonThink
Reviewer: @shixinzhang
In the factory method mode, specific factories are responsible for producing specific products, and each specific factory corresponds to a specific product. Factory methods are also unique. Generally, there is only one factory method or a group of overloaded factory methods in a specific factory. We can think of times when we need a factory that can provide multiple product objects, rather than a single product object, called a product family. For example, in the factory method, HTTPFactory is only responsible for producing HTTPProtocol and so on. It’s obviously not meeting our needs. What should we do? The abstract factory pattern solves this problem for us.
To give you a better understanding of the abstract factory pattern, let’s first understand the following probabilities:
-
Product grade structure: The product hierarchy structure is the inheritance structure of the product. For example, an abstract class is Protocol, and its subclasses are HTTPProtocol, TCPProtocol, and UDPProtocol. Then the abstract Protocol and the specific Protocol form a product hierarchy. Procotol is a subclass of the protocol.
-
Product family: In the abstract factory model, product family refers to a group of products produced by the same factory and located in different product grade structures, such as The Big MAC and Cola produced by McDonald’s in China. The Big MAC is located in the hamburger product grade structure, while the Cola is located in the beverage product grade structure.
define
Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Provides an interface for creating a set of related or interdependent objects without specifying their concrete classes.
The instance
We all go to McDonald’s to eat hamburgers, and McDonald’s products in China and foreign McDonald’s, it is different, at least the raw materials are different, the chicken is local, and the size of the hamburger is not the same. The variety of set meals is not like it, let’s use McDonald’s for example.
Class diagram:
Code:
AbsFactory
public interface AbsFactory { Hamburg createhamburg(); Drink createDrink(); / / set... }Copy the code
Store
public class Store { public static void main(String[] args) { AbsFactory factory = new ChineseFactory(); factory.createDrink(); factory.createhamburg(); factory = new NYFactory(); factory.createDrink(); factory.createhamburg(); }}Copy the code
Running results:
Advantages:
-
Encapsulation, the implementation classes of each product are not related to high-level modules, what is the concern? It’s an interface, it’s an abstraction, it doesn’t care how the object is created, who’s responsible for that? The factory class.
-
It is convenient to add new specific factories and product families without modifying existing systems, in accordance with the “open closed principle”.
-
The abstract factory pattern isolates the generation of concrete classes so that customers do not need to know what is being created. Because of this isolation, it is relatively easy to change a specific plant. All concrete factories implement the common interfaces defined in the abstract factory, so simply changing an instance of the concrete factory can change the behavior of the entire software system to some extent. In addition, abstract factory pattern can achieve high cohesion and low coupling design purpose, so abstract factory pattern has been widely used.
Disadvantages:
-
The biggest disadvantage of abstract factory mode is that it is very difficult to expand the product family. For example, if new products are added in the instance, corresponding products need to be added in the abstract factory class, and the open-close principle is inclined.
Usage scenarios
-
The abstract factory pattern is used when the concrete products that the factory provided by the system needs to produce are not a simple object, but multiple concrete products of different types in different product hierarchies.
-
There is more than one product family in the system and only one of them is used at a time.
-
The system provides a library of product classes, all of which appear in the same interface, so that the client is implementation-independent.
For example, when an APP has multiple themes, such as youth version, elderly version, narcissism, etc., abstract factory mode can be considered.
The difference and relation between abstract factory pattern and factory method pattern
The main difference between the abstract factory pattern and the factory method pattern is that the factory method pattern targets at one product level structure, while the abstract factory pattern needs to face multiple product level structures. One factory level structure can be responsible for the creation of product objects in multiple different product level structures. The abstract factory pattern is simpler and more efficient than the factory method pattern when a factory hierarchy can create all objects in a product family that belong to different product hierarchies.
They have no absolute boundaries themselves. When each concrete factory class in the abstract factory pattern creates only one product object, that is, there is only one product hierarchy structure, the abstract factory pattern degenerates into the factory method pattern. The factory method pattern degenerates into a simple factory pattern when abstract factories are merged with concrete factories in the factory method pattern, providing a unified factory to create product objects, and designing the factory method for creating objects as a static method.
Android source code application
In the operation of Java connecting database, the object operation family formed by the operation of different database is a good application of abstract factory mode. In the Android WebViewFactoryProvider/WebViewChromiumFactoryProvider also used the abstract factory.
WebViewFactoryProvider
public interface WebViewFactoryProvider { ... WebViewProvider createWebView(WebView webView, WebView.PrivateAccess privateAccess); . CookieManager getCookieManager(); }Copy the code
WebViewChromiumFactoryProvider
public class WebViewChromiumFactoryProvider implements WebViewFactoryProvider { @Override public WebViewProvider createWebView(WebView webView, WebView.PrivateAccess privateAccess) { WebViewChromium wvc = new WebViewChromium(this, webView, privateAccess); . return wvc; } @Override public CookieManager getCookieManager() { synchronized (mLock) { if (mCookieManager == null) { ... mCookieManager = new CookieManagerAdapter(new AwCookieManager()); } } return mCookieManager; }... }Copy the code
WebViewFactoryProvider (WebViewFactoryProvider) is an abstract factory interface that defines the creation methods of objects required by WebView and its peripheral functions. Most of these methods are factory methods, and the object types returned by them are abstract and belong to the interface oriented programming style. Abstract Factory WebViewFactoryProvider creates the following objects:
Implement the core functions of Webview WebViewProvider, CookieManager management Cookie, GeolocationPermissions and WebviewDatabase for storing Web form data, among others.
WebViewChromiumFactoryProvider is the specific implementers of the abstract factory, it provides “Chromium” theme of product family, also is currently the latest version of the Webview is using product family. If a new browser engine kernel comes out in the future, you can simply create a product based on the WebViewFactoryProvider interface. Then replace the existing “Chromium” to complete the upgrade without changing any code anywhere else.
PS:
For other design patterns, see “Read the original”. The project is ongoing and will continue to be updated.