In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to implement the factory method pattern in C #, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Hu's introduction to the factory method model
2.1.Motivity (Motivate)
In the process of building a software system, we are often faced with the creation of "some object": due to the change of requirements, this object is often faced with drastic changes, but it has a relatively stable interface.
How to deal with this change? How to provide an "encapsulation mechanism" to isolate the changes of "this volatile object", so as to keep the "objects of other dependent objects" in the system from changing with the requirements?
2.2, intention (Intent)
Define an interface for creating objects, and let the subclass decide which class to instantiate. Factory Method delays the instantiation of a class to a subclass. -- Design pattern GoF
2.3.Structure diagram (Structure)
2.4. The composition of the model
As you can see, the structure diagram of the factory method pattern has the following roles:
(1) abstract factory role (Creator): acts as an abstract factory, defining the basic operations of a factory class that any concrete factory must inherit.
(2) concrete factory role (ConcreteCreator): acts as a concrete factory role. This class must inherit the abstract factory role and implement the methods defined by the abstract factory, which are used to create concrete products.
(3) abstract product role (Product): acts as an abstract product role, defining all the basic operations of the product type, and the concrete product must inherit the abstract class.
(4) specific product roles (ConcreteProduct): act as specific product roles, implement abstract methods defined by abstract product classes, created by concrete factory classes, and there is an one-to-one corresponding relationship between them.
2.5. Code implementation of factory method pattern
The problem with [simple factory pattern] is that if there are new requirements, you need to modify the implementation code of the method that creates an instance of the product object in the factory class, and one of the principles of object-oriented design is that I encapsulate where there are changes. There are two other big principles, one is that for abstract programming, details and high-level implementations rely on abstraction, and the second principle is: more composition, less inheritance. These three principles are the most fundamental principles, and learning design patterns must be based on these three principles, otherwise they are all in vain. According to these three principles, six specific principles are derived, namely, the single responsibility principle, the Richter substitution principle, the dependency inversion principle, the interface isolation principle, the Dimitt rule and the opening and closing principle. since the factory class has changed, we encapsulate it for abstract programming, we first abstract a factory base class, and then each requirement implements a concrete factory class. In this way, we comply with the [opening and closing principle OCP] and let a factory produce a product and correspond to it one by one. We postpone the creation of specific products to subclasses, where the factory class (which is the base class) is no longer responsible for the creation of all products, but only gives the interfaces that the specific factory must implement. In this way, the factory method pattern allows the system to add new products without modifying the factory class logic, thus overcoming the shortcomings of the simple factory pattern. The following is the implementation code for the factory method pattern:
In the factory method pattern, we also abstract the car class into an abstract base class, which coincides with abstract programming, and the client does not rely on a specific car when using it. Using the factory method to implement the system, if the system needs to add new products, we can use polymorphism to complete the expansion of the system, for the abstract factory class and the code in the concrete factory does not need to make any changes. For example, if we want to add a Mercedes-Benz, we only need to inherit a BenChiCar class from the Car abstract class and a "Mercedes-Benz" factory class BenChinaCarFactory from the Factory abstract base class, so the extension accords with the principle of OCP. The specific code is:
III. Several key points of Factory Method model
The Factory Method pattern is mainly used to isolate the coupling relationship between consumers and concrete types of class objects. In the face of a specific type that changes frequently, tight coupling will lead to the fragility of the software.
Through the object-oriented approach, the Factory Method pattern delays the work of the specific objects to be created to subclasses, thus implementing a strategy of extension (rather than change), which better solves this tight coupling relationship.
Factory Method pattern solves the requirement change of "single object"
AbstractFactory pattern solves the demand change of "series objects"
Builder pattern solves the requirement change of "object part"
3.1]. Advantages of the factory method model:
(1) in the factory method, users only need to know the specific factory of the product they want, regardless of the specific creation process, or even the class name of the specific product class.
(2) when adding new products to the system, we only need to add a specific product category and the corresponding implementation factory, without any modification to the original factory, which is well in line with the "opening and closing principle".
3.2], shortcomings of the factory method model:
(1) each time you add a product, you need to add a specific class and object implementation factory, so that the number of classes in the system increases exponentially, which not only increases the complexity of the system to a certain extent, but also increases the dependence of the specific classes of the system. This is not a good thing.
3.3], scenarios used in factory method mode:
(1) A class that does not know the object it needs. In the factory method pattern, we don't need the class name of the specific product, we just need to know the specific factory that created it.
(2) A class specifies which object to create through its subclass. In the factory method pattern, the abstract factory class only needs to provide an interface to create the product, and its subclass determines the specific object to be created, and when the program runs, the subclass object will override the parent class object, which makes the system easier to extend.
(3) delegate the task of creating an object to one of several factory subclasses, and the client can use it without concern about which factory subclass creates the product subclass, and then dynamically specify it when needed.
4. Classes that implement factory methods in .NET.
There are also many classes in the .NET class library that implement factory methods. For example, in Asp.net, the handler object is specifically used to handle requests. When we request a * .aspx file, it will be mapped to the System.Web.UI.PageHandlerFactory class for processing, and the request for * .ashx will be mapped to the System.Web.UI.SimpleHandlerFactory class (both classes inherit from the IHttpHandlerFactory interface). On this point, we can find the relevant definitions in the "C:\ Windows\ Microsoft.NET\ Framework\ v4.0.30319\ Config\ Web.Config" file, as follows:
There is a screenshot of the configuration file, so you can study it yourself when you have time.
Let's take a look at how the factory method pattern is implemented in Asp.net. If a request is made to an Index.aspx page, the GetHandler method in PageHandlerFactory will be called to create an Index.aspx object. The class diagram relationship between them is as follows:
This is the answer to the question about how to realize the factory method 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.