First, Introduction
In the previous topic, we introduced the factory method pattern, which was designed to overcome the shortcomings of the simple factory model, the factory class of the simple factory model will need to add additional code as the product class increases, and the factory method pattern has a very good extensibility for each specific factory class that only completes the creation of a single instance. But in real life, a factory only creates a single product such examples are very few, because now the factory is diversified, a factory to create a series of products, if we want to design such a system, the factory method pattern is obviously not applicable here, and then the abstract factory model can be a good solution to a series of product creation problems, This is what the topic is about.
Second, the abstract factory detailed introduction
This is the first example of an abstract factory in life to implement an abstract factory, and then give the abstract factory definition and UML diagram to help you better grasp the abstract factory model, and when you understand, you can compare abstract factory life examples of implementation and its definition to deepen the understanding of the UML diagram of the abstract factory.
2.1 Concrete implementation of the abstract factory
The following is an example of a " taste " chain in life to achieve an abstract factory model. For example, the Taste duck neck want to open branches in Jiangxi Nanchang and Shanghai, but because the local tastes are different, in Nanchang, all the taste of the things will do a little spicy, and Shanghai does not like to eat spicy, so Shanghai's all the absolutely taste of things will not do like Nanchang's so spicy, However, this difference led to the Nanchang taste of the factory and Shanghai's absolute taste of the production of all the flavor of the product is different, that is, a specific factory to be responsible for a series of products (refers to the taste of all food) the creation of work, the following concrete see how to use the abstract factory model to achieve this situation.
1//<summary> 2///below take a taste duck neck chain as an example of the following abstract Factory mode 3///Because each place likes the taste is not the same, some places like spicy point, some places like to eat not spicy point 4//Client call 5//</summary> 6 class Client 7 {8 static void Main (string[] args) 9 {10 Nanchang Factory production of Nanchang duck neck and duck rack one by one abstractfactory nanchangfactory = new Nanchangfactory (); Yabo Nanchangyabo = Nanchangfactory.createyabo (); Nanchangyabo.print (); Yajia nanchangyajia= Nanchangfactory.createyajia (); Nanchangyajia.print (); 16 17//Shanghai factory production of duck neck and duck rack in Shanghai abstractfactory shanghaifactory = new Shanghaifactory (); Shanghaifactory.createyabo (). Print (); Shanghaifactory.createyajia (). Print (); Console.read (); +/-<summary> 27///Abstract factory class, providing the interface for creating two different places for duck and duck necks.//</summary> 29 Public abstract class Abstractfactory 30 {31///abstract factory provides access to a series of products that createThe mouth, here as an example, gives only the creation interface of the duck neck and the duck rack in the absolute taste. Public abstract Yabo Createyabo (); Public abstract Yajia Createyajia (); +//<summary> 37///Nanchang Taste Factory is responsible for the production of Nanchang duck neck and duck rack to///</summary> the public class NanC Hangfactory:abstractfactory 40 {41//Make Nanchang Duck neck public override Yabo Createyabo () 43 {44 return new Nanchangyabo (); 45} 46//Production Nanchang Duck rack-public override Yajia Createyajia () Nchangyajia (); */<summary> 54///Shanghai Taste Factory is responsible for the production of Shanghai Duck Neck and duck rack///</summary> Publ IC class Shanghaifactory:abstractfactory 57 {58//Make Shanghai Duck Neck public override Yabo Createyabo () 60 {Shanghaiyabo return new (); 62} 63//Production Shanghai Duck Stand public override Yajia Crea Teyajia () () (), {Shanghaiyajia (); 67} 68} 69 70 <summary> 71//Duck Neck abstract class, for each place duck neck class inherit///</summary> Public abstract class Yabo 74 {$//<summary> 76///printing method for output information//</summary> + public abstract Vo ID Print (); +/-<summary> 82///Duck Rack abstract class for each place's duck rack class inherit///</summary> public abstract CL Yajia//<summary> 87///printing method for output information///</summary> PU Blic abstract void Print (); *//<summary> 93///Nanchang Duck neck class, because Jiangxi people like to eat spicy, so Nanchang duck neck slightly more than Shanghai do spicy 94///</summary> P Ublic class Nanchangyabo:yabo $ {$ public override void Print () 98 {Console.Write Line ("The duck Neck of Nanchang"),}101}102 103///<summary>104//Shanghai Duck Neck do not have a duck neck made in Nanchang spicy///</summary> 106 public class Shanghaiyabo:yabo107 {108 public override void Print () 109 {110 Console.WriteLine ("Shanghai Duck Neck"), 111}112}113///<summary>115//Nanchang Duck Rack 116/// lt;/summary>117 public class nanchangyajia:yajia118 {119 public override void Print () 120 {121 Console.WriteLine ("Duck shelves in Nanchang"); 122}123}124///<summary>126//SHANGHAI Duck Rack 127// </summary>128 public class Shanghaiyajia:yajia129 {$ public override void Print () 131 {1 Console.WriteLine ("Shanghai Duck Shelf"); 133}134}
2.2 Definition and class diagram of the abstract factory pattern
There are detailed comments in the code above, the above code is no longer explained, the following is a concrete look at the definition of the Abstract Factory mode (understanding the definition can refer to the above implementation to deepen understanding):
Abstract Factory mode: Provides an interface to create a product that is responsible for creating related or dependent objects without specifically specifying the specific class
Abstract factories allow customers to use abstract interfaces to create a set of related products without needing to know or care what the actual product is. This allows the customer to be decoupled from the specific product. The following is a class diagram of the abstract work pattern to understand the relationships among the classes:
2.3 Abstract plant response to demand changes
After reading the above abstract factory implementation, if the "taste" companies want to open a branch in Hunan how to do? Because Hunan people like to eat spicy, the following specific look at the application of the abstract factory model system is how to respond to this demand.
<summary>// if the taste and want to open a Hunan branch, because Hunan likes to eat hemp// so it is necessary to have a Hunan factory specializing in the production/// </summary> public class Hunanfactory:abstractfactory { //make Hunan Duck Neck public override Yabo Createyabo () { return New Hunanyabo (); } Production of Hunan Duck Rack public override Yajia Createyajia () { return new Hunanyajia (); } } <summary>//Hunan Duck Neck//// </summary> public class Hunanyabo:yabo { public override void Print () { Console.WriteLine ("Duck Neck in Hunan"); } <summary>/// Hunan Duck Racks/// </summary> public class Hunanyajia:yajia { public override void Print () { Console.WriteLine ("Duck Shelf in Hunan"); } }
At this point, you only need to add three classes: one is Hunan specific factory class, is responsible for the creation of Hunan Taste duck neck and duck rack, and the other two class is a Hunan taste of duck neck class and Duck rack class. As seen from the above code, abstract factory for the changes in the series of products to support the "open-closed" principle (refers to the system to expand open, to modify the closed), the extension is very simple, but the abstract factory to add new products This situation does not support the "open-closed" principle, which is the shortcomings of the abstract factory This will be described in detail in part fourth.
Iii. analysis of the abstract factory
Abstract Factory mode delays the creation of specific products into sub-classes of specific factories, thus encapsulating the creation of objects, reducing the dependence between the client and the specific product class, so that the system coupling is low, which is more conducive to later maintenance and expansion, which is really the advantage of abstract Factory mode, Then the abstract pattern also has a shortage of places. The following is a concrete look at the shortcomings of the abstract factory (the shortcomings in fact in the previous introduction to have been involved in):
Abstract Factory mode is difficult to support the change of new kinds of products. This is because the abstract factory interface has identified a collection of products that can be created, and if a new product needs to be added, it is necessary to modify the interface of the abstract factory, which involves the transformation of the abstract factory class and all subclasses, thus violating the "develop-close" principle.
Once you know the pros and cons of an abstract factory, you'll be able to take a good look at what you're thinking about using the abstract factory model, and here's a concrete view of how a system using the abstract factory model should meet those prerequisites:
- A system does not require reliance on how product class instances are created, composed, and expressed, and this is a prerequisite for all plant model applications.
- The system has a number of products, and the system only consumes one of the products
- The system requires a library of product classes, all products appear on the same interface, and the client does not need to rely on a specific implementation.
Four. NET in the abstract factory pattern implementation
The application of abstract factory patterns in practice is also quite frequent, however in our. NET class Library also has a class that applies the abstract factory pattern, which is System.Data.Common.DbProviderFactory, which is in the System.Data.dll assembly, This class plays the role of abstract factories in abstract Factory mode, and we can use the Reflector Anti-compilation tool to see the implementation of this class:
Play the role of an abstract factory
Creates a collection of objects that are required to connect to the database,
This object collection includes the DbConnection object (which is an abstract product class, such as the Yabo class in the flavor example), the DbCommand class, and the DbDataAdapter class, which are required to implement the method in this abstract class for different specific factories.
Public abstract class dbproviderfactory{ //provides an interface method for creating a specific product protected dbproviderfactory (); Public virtual DbCommand CreateCommand (); Public virtual DbCommandBuilder createcommandbuilder (); Public virtual DbConnection createconnection (); Public virtual Dbconnectionstringbuilder createconnectionstringbuilder (); Public virtual DbDataAdapter createdataadapter (); Public virtual Dbdatasourceenumerator createdatasourceenumerator (); Public virtual DbParameter CreateParameter (); Public virtual CodeAccessPermission createpermission (PermissionState state);
The DbProviderFactory class is an abstract factory class that provides an interface to the collection of objects needed to create a database connection, the work actually created in its subclass factory, and Microsoft using a SQL Server database, thus providing a connection to the SQL The specific factory implementation of the server data, the specific code can be viewed with the anti-compilation tool, the specific code is as follows:
Act as a factory-specific role to create the objects required to connect SQL Server data public sealed class Sqlclientfactory:dbproviderfactory, iserviceprovider{//F Ields public static readonly sqlclientfactory Instance = new SqlClientFactory (); Constructor Private SqlClientFactory () {}//override method in abstract factory public override DbCommand CreateCommand () {// Create a specific product return new SqlCommand (); } public override DbCommandBuilder Createcommandbuilder () {return new SqlCommandBuilder (); } public override DbConnection CreateConnection () {return new SqlConnection (); } public override Dbconnectionstringbuilder Createconnectionstringbuilder () {return new SQLCONNECTIONSTRINGB Uilder (); } public override DbDataAdapter Createdataadapter () {return new SqlDataAdapter (); } public override Dbdatasourceenumerator Createdatasourceenumerator () {return Sqldatasourceenumerator.instan Ce } public override DbParameter CreateParameter () { return new SqlParameter (); } public override CodeAccessPermission CreatePermission (PermissionState state) {return new Sqlclientpermissi On (state); }}
Because Microsoft only gives implementations of specific factories that connect to SQL Server, we can also customize the implementation of specific factories that connect to Oracle and MySQL.
V. Summary
Here, the introduction of the abstract factory model is over, and the next topic will introduce you to the construction model.
C # Design Pattern-Abstract Factory mode