Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Abstract Factory pattern in C #

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In view of how to implement abstract factory pattern in C#, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a simpler and easier way.

Detailed introduction of Abstract Factory

2.1.motivation (Motivate):

In software systems, we are often faced with the creation of "a system of interdependent objects". At the same time, due to the change of requirements, there are often more series of object creation work. How to deal with this change? How to bypass the conventional object creation method (new) and provide an "encapsulation mechanism" to avoid the tight coupling between client programs and this "multi-series concrete object creation effort"?

2.2. Intention (Intent):

Provides an interface for creating a series of related or interdependent objects without specifying their specific classes. -- Design pattern GoF

2.3.Structure diagram (Structure)

This diagram is the UML diagram of the abstract factory. We understand the pattern by combining the intention, motivation and diagram of the abstract factory. Today we will take the construction of a house as an example to illustrate the implementation mechanism of the abstract factory.

2.4. The composition of the model

As you can see, the structure diagram of the abstract factory pattern has the following roles:

(1) Abstract product class roles (AbstractProduct): define abstract interface objects for each product that is interdependent in an abstract factory. It can also be said that if there are several products, several abstract roles should be declared, and each abstract product role matches a specific product.

(2) concrete product class (ConcreteProduct): the concrete product class implements the abstract product class, which is the type of implementation for a specific product.

(3) Abstract factory class role (Abstract Factory): defines the interface operation to create a set of interdependent product objects, each operation corresponds to each product one by one.

(4), concrete factory class role (ConcreteFactory): implement all the abstract interface operations in the abstract class, and you can create a series of concrete products, which are subclasses of the "abstract product class role".

2.5. Concrete code implementation of abstract factory

As we get older, we are old enough to get married. The first problem of marriage is the house problem, suppose I have a very rich father, , money can solve a lot of problems. As the eldest son, I hope to have an European-style house, coupled with pastoral scenery, this life is enough. My brother is different. he wants a modern house. If my brothers and sisters had been a little longer, they would have asked for more. Since the house consists of roofs, floors, windows and doors, other components are temporarily omitted, there are so many houses to build, and the roofs, floors, windows and doors of each house are all in one system. let's look at how to use the Abstract Factory pattern to build different houses.

2.6. Abstract factories respond to changes in requirements

Let's take a look at how this model responds to changes in demand, assuming that my cousin wants a classical house when he looks at our house (, this guy has a lot of things, and good things can't fall behind him).

At this point, only five categories need to be added: one is the classical-style factory class, which is responsible for creating classical-style houses, and the other are specific products of classical-style roofs, floors, windows and doors. As can be seen from the above code, the abstract factory supports the "open-closed" principle for changes in the series of products (which requires the system to be open to extension and closed to modification), and it is very easy to expand. However, the abstract factory does not support the "open-closed" principle for adding new products, because it is necessary to modify the abstract base class AbstractFactory that creates the series of products and add the creation method of the corresponding products. This is also the disadvantage of abstract factories.

Third, the main points of the implementation of the abstract factory

1. If you do not respond to the changes in the requirements of "multi-series object creation", there is no need to use the AbstractFactory pattern, which is fine with a simple static factory.

2. "Series objects" refers to the interdependence or interaction between these objects, such as the dependence of "roads" and "houses", and the dependence of "roads" and "tunnels" in game development scenarios.

3. The main purpose of AbstractFactory model is to cope with the demand changes of "new series". Its disadvantage is that it is difficult to cope with the changes in the demand of "new objects".

4. AbstractFactory mode often drinks the combination of FactoryMethod mode to cope with the demand change of "object creation".

3.1], the advantages of abstract factory pattern: the [abstract factory] pattern delays the creation of a series of products to a subclass of a specific factory, and we declare factory class variables using abstract types, by the same token, we use product types as abstract types, which can reduce the dependence between client code and specific product classes as much as possible, thus reducing the coupling of the system. Reduced coupling is more beneficial for later maintenance and extension, which is the advantage of the Abstract Factory pattern. Some people might say that in the Main method (the code here is the user of the client), the specific factory class will still be used, yes. In fact, we move this part out through the configuration of Net, and finally put the dependencies in the configuration file. If there are new requirements, we only need to modify the configuration file, there is no need to modify the code at all, to make the customer code more stable. Dependencies are bound to exist, and what we need to do is to reduce them. It is difficult and unrealistic to get rid of them completely.

3.2], the disadvantages of abstract factory model: if there are advantages, there must be disadvantages, because each model has its own scope of use, or the problem to be solved, the problem that can not be solved is shortcomings, which can not be called shortcomings. The [Abstract Factory] pattern is difficult to support the change of adding new products, because the collection of products that can be created has been identified in the abstract factory interface, and if you need to add new products, you must modify the interface of the abstract factory. This involves changes to the abstract factory class and all subclasses, which violates the "development-closed" principle.

3.3], abstract factory pattern usage scenario: if the system requires multiple code solutions, and there are many interrelated product types in each code scheme, and we can use this pattern when we can use a set of products in the system instead of each other, the client does not need to rely on the specific implementation.

4. Implementation of Abstract Factory pattern in .NET

Microsoft's class library has been developed for so many years, there are a large number of applications of design patterns in it, and the Abstract Factory pattern is also widely used in .NET class libraries, such as types related to manipulating the database. This class is System.Data.Common.DbProviderFactory, which is located in the System.Data.dll assembly. This class acts as an abstract factory in the abstract factory pattern, and we can use the ILSpy decompiler to view the implementation of this class:

/ / play the role of abstract factory

/ / create a collection of objects needed to connect to the database

/ / this set of objects includes DbConnection objects (this is an abstract product class, such as the YaBo class in the tasteless example), DbCommand class, and DbDataAdapter class. Methods in this abstract class need to be implemented for different concrete factories.

The DbProviderFactory class is an abstract factory class, which provides the interface for creating a collection of objects needed to create a database connection. The actual creation is carried out in its subclass factory. Microsoft uses SQL Server database, so it provides a concrete factory implementation for connecting SQL Server data. The specific code can be viewed with decompiler, as shown below:

SqlClientFactory acts as a concrete factory to create objects needed to connect to SQL Server data

DbcFactory is also a specific factory class

This is the answer to the question about how to implement the abstract factory pattern in C#. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report