I. Introduction
The factory method model was introduced in the previous topic. The factory method model was designed to overcome the disadvantages of the simple factory model, the factory class in the simple factory mode needs to add additional code as the product class increases), while the factory method mode can only create a single instance for each specific factory class, so it has good scalability. However, in real life, there are few examples of creating a single product in a factory, because the current factories are diversified, and a factory creates a series of products, if we want to design such a system, the factory method mode is obviously not applicable here, and then the abstract factory mode can solve a series of product creation problems, which is what we will introduce in this topic.
Ii. Abstract Factory details
Here we will first use an example of abstract factory in life to implement an abstract factory, and then give the definition of abstract factory and UML diagram to help you better master the abstract factory model, at the same time, you can better understand the UML diagram of the abstract factory by comparing the implementation of the example in the abstract factory life and its definition.
2.1 Specific implementation of the abstract factory
Next we will take"Flavor"Chain stores to implement an abstract factory model. For example, you want to open a branch in Nanchang and Shanghai, Jiangxi province, but because the tastes of local people are different, all the delicious things in Nanchang will be spicy, while Shanghai doesn't like spicy things, therefore, all the things in Shanghai are not as spicy as those in Nanchang. However, this difference leads to the Difference Between Nanchang's flavor factory and Shanghai's flavor factory to generate all unique products, that is to say, a specific factory is responsible for the creation of a series of products (that is, all the delicious food). Let's take a look at how to use the abstract factory model to achieve this situation.
Main( AbstractFactory nanChangFactory = YaBo nanChangYabo = YaJia nanChangYajia= AbstractFactory shangHaiFactory = Console.WriteLine( Console.WriteLine( Console.WriteLine( Console.WriteLine( }
2.2 Abstract Factory schema definition and class diagram
The above code has detailed comments, so I will not explain the above Code here. Let's take a look at the definition of the abstract factory model (for understanding the definition, refer to the above implementation to deepen understanding ):
Abstract Factory mode: provides an interface for creating products to create related or dependent objects without specifying specific classes.
Abstract Factory allows customers to use abstract interfaces to create a group of related products without knowing or caring about the specific products actually produced. In this way, the customer can be decoupled from a specific product. The following describes the relationships between classes by abstracting the class diagrams of the work mode:
2.3 abstract factories respond to demand changes
After reading the implementation of the abstract factory above, what if the company wants to open another branch in Hunan? Because Hunan people like to eat spicy food, let's take a look at how the abstract factory model system is applied to meet this demand.
At this time, you only need to add three categories: one is a specific factory type in Hunan, responsible for creating a duck neck and duck shelf in Hunan taste, and the other two categories are duck neck and duck shelf with Hunan taste. From the code above, the abstract factory supports the "open-closed" principle for changes to the series of products (that is, the system is required to be open to expansion and closed to modification), which is very easy to expand, however, the abstract factory does not support the "open-close" principle for adding new products, which is also a disadvantage of the abstract factory. This will be detailed in Part 4.
3. Abstract Factory Analysis
The abstract factory mode delays the creation of a specific product to the subclass of a specific factory. In this way, the creation of an object is encapsulated to reduce the dependency between the client and the specific product class, thus reducing the Coupling Degree of the system, this is more conducive to later maintenance and expansion. This is the advantage of the abstract factory model, and there are also deficiencies in the abstract mode. Next, let's take a look at the shortcomings of the abstract factory (the shortcomings are already covered in the previous introduction ):
Abstract Factory models are difficult to support changes in new types of products. This is because the abstract factory interface has determined the product set that can be created. To add a new product, you must modify the abstract factory interface, this involves changes to abstract factory classes and all subclasses, which violates the "development-closed" principle.
After knowing the advantages and disadvantages of the abstract factory, we can grasp the situation and consider the use of the abstract factory model. Next we will take a look at the specific conditions that the system using the abstract factory model should meet:
- A system does not need to rely on how product instances are created, combined, and expressed. This is also the prerequisite for all factory models.
- This system has multiple series of products, and the system only consumes a series of products.
- The system requires a product library. All products use the same interface, and the client does not need to rely on the specific implementation.
IV. Implementation of the abstract factory model in. NET
The abstract factory mode is frequently used in practice. However, in our. NET class library, there are classes that use the abstract factory mode. This class is System. Data. Common. DbProviderFactory.This class is located in the System. Data. dll Assembly. This class plays the role of the abstract factory in the abstract factory mode. We can use reflector decompilation tool to view the implementation of this class:
/// Set of objects required for database connection Creation,
/// This object set includes the DbConnection object (this is an abstract product class, such as the YaBo class in the tasteless example), The DbCommand class, And the DbDataAdapter class, methods In this abstract class must be implemented for different factories,
DbProviderFactoryA class is an abstract factory class that provides an interface for the object set required to create a database connection. The actual creation work is carried out in its subclass factory, microsoft uses the SQL Server database, so it provides the specific factory implementation for connecting SQL Server data. The specific code can be viewed using the decompilation tool. The specific code is as follows:
SqlClientFactory Instance =
Because Microsoft only provides the implementation of a specific factory connected to SQL Server, we can also customize the implementation of a specific factory connected to Oracle and MySql.
V. Summary
Here, the introduction of the abstract factory model is over. The next topic will introduce the construction model.
Program source code: Abstract Factory mode implementation