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 simple Factory pattern in C #

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to realize the simple factory model in C#. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Introduction of simple factory model

Whether it is a simple factory or a complex factory, first of all, they are all factories. what does a factory do? In real life, factories are responsible for producing products, which can be eaten or used, and can provide us with function or supplementary energy. this product is useful and real. So what does the factory mean in object-oriented software design? Since it is also a factory, it must also produce something, but this thing must be an instance of an object here, and we can use it. So in software design, the concept of factory refers to a type that can produce an instance of an object of a certain type. Just like if we want to eat cookies, we don't have to make them ourselves, we just need to go to the supermarket to buy them, because some factories have already produced cookies, production is the task of the factory, and eating is our own business. It's nice that the two don't interfere with each other. In software design, if we want to use a certain type of instance in the future, just tell the factory, and he will give us the instance object we want, and we don't have to worry about how the factory produces it, and we don't have to worry about it, which is the so-called decoupling. In our usual programming, when we create an object using the "new" keyword, the class depends on the object, that is, the coupling between them is relatively high. When the requirements change, we have to modify the source code of this class. At this time, we can use the very important principle of object-oriented (OO) to solve this problem, which is to encapsulate the point of change, since we want to encapsulate the point of change. Naturally, it is necessary to find the changed code, and then encapsulate the changed code with classes, which is the way to implement our simple factory pattern. The following leads to a simple factory pattern through a real-life example.

In life, we often have to eat out. Of course, we can also cook at home, but it is troublesome to cook and eat by ourselves, because we have to buy food by ourselves. However, there is no such trouble at all when we go out to eat. We just need to go to the restaurant to order, and the food shopping can be left to the restaurant, where the restaurant acts as a simple factory. Let's take a look at how real-life examples are represented in code.

The situation of cooking by yourself:

When we cook for ourselves, if we want to eat other dishes, we need to buy this kind of food and wash it. With a restaurant (that is, a simple factory), we can leave these operations to the restaurant. At this time, the dependence of consumers (that is, us) on food (that is, specific objects) has changed from direct to indirect. This is another object-oriented principle-reducing the coupling between objects. Let's take a look at the implementation code after having a restaurant (that is, the implementation of a simple factory):

Advantages and disadvantages

After reading the implementation of the simple factory pattern, many people must be confused-we just moved the changed code to the factory class, as if there is no problem of change, if the customer wants to eat other dishes, we still need to modify the methods in the factory class (that is, add more case statements, before applying the simple factory pattern, modify the customer class). First of all, I would like to say: you are right that each design pattern will only solve one problem, they have their own usage scenarios, and no one pattern can solve all the problems. This is the disadvantage of the simple factory pattern (the factory method pattern described later can be well solved). However, the simple factory pattern and the previous implementation also have its advantages:

The simple factory pattern solves the problem that the client directly depends on the specific object, and the client can eliminate the responsibility of creating the object directly, instead of just consuming the product. The simple factory model realizes the division of responsibility.

The simple factory pattern also plays a role in code reuse, because in the previous implementation (in the case of self-cooking), a different person also has to implement the method of cooking in his own class, and then with a simple factory, everyone who goes to a restaurant doesn't have to bother so much, just be responsible for consumption. At this time, the cooking method of the simple factory is shared by all customers. (at the same time, this is also a disadvantage of the simple factory method-- because the factory class centralizes all the product creation logic, once it does not work properly, the whole system will be affected, and it is not difficult to understand, just as things have two sides.)

Although the shortcomings of the simple factory model have been described above, here is a summary of the disadvantages of the simple factory model:

The factory class centralizes all the product creation logic, and once it doesn't work properly, the whole system will be affected (popular meaning: once the restaurant is out of food or closed, many people who don't want to cook will have nothing to eat.)

It is difficult to expand the system, and once a new product is added, the factory logic has to be modified, which will cause the factory logic to be too complex.

After understanding the advantages and disadvantages of the simple factory model, we can then know the application scenario of the simple factory:

Consider using a simple factory pattern when the factory class is responsible for creating fewer objects

If customers only know the parameters passed in to the factory class, they can consider using the simple factory pattern if they are not concerned about the logic of how to create the object.

4. UML diagram of simple factory model

In many cases, the simple factory pattern can be called the static factory pattern (because the factory class defines a static method). It is up to a factory class to decide which instance of the product class to create based on the passed parameters (popular point expression: responsible for cooking that dish through the customer's order). The UML diagram of the simple factory pattern is as follows:

If you want to see the source code, the source code is as follows:

Fifth, the implementation of simple factory pattern in .NET.

After introducing the simple factory pattern, there is a similar implementation in the .NET class library. The System.Text.Encoding class in NET implements the simple factory pattern, and the GetEncoding (int codepage) in this class is the factory method. The specific code can be viewed through the Reflector decompiler tool.

The UML diagram of Encoding in .NET is as follows:

The simple factory pattern implemented in the Encoding class is an evolution of the simple factory pattern, where the simple factory class is played by abstract products, but how does the Encoding class in .NET solve the problems in the simple factory pattern (that is, what if a new code is added)? The switch function in the GetEncoding method has the following code:

There is some instantiated code that is not commonly coded in the GetEncodingRare method, and Microsoft officially uses this method to solve the problem of adding a new kind of coding. (that is, listing all possible coding situations), the reason why Microsoft solves this problem in this way may be that the code is now stable and the possibility of adding new code is relatively low. so this part of the code has not been changed in .NET 4.5.

The above is the editor for you to share how to achieve the simple factory model in C#, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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