Replace the switch Branch (14) and switch branch of the factory with reflection technology
First of all, I wish you a happy New Year and good luck.
Well, we have talked a lot about the factory model, which has a very obvious feature. There is a method in the factory that contains many swich case branches, as we mentioned earlier, we can use reflection technology to avoid swich case Branch. OK, so today, we will use the reflection + abstract factory model to improve the abstract factory model.
First, let's review what reflection is: Assembly. Load ("Assembly name"). CreateInstance ("namespace. Class Name"); this is its syntax. Using System. Reflection;
Previously we wrote IUser result = new SqlserverUser ();
Using Reflection, we can use IUser result = (IUser) Assembly. Load ("Assembly name"). CreateInstance ("namespace. Class Name"); this is its syntax. Using System. Reflection;
The actual results are the same, but are there any differences between the two statements?
The SqlserverUser class is also instantiated. The difference between the two statements is that the SqlserverUser class appears in the statement as a string variable. In this way, for a string, that is very flexible. This is equivalent to writing to a program, and a variable appears in the program, which is more flexible and self-evident.
In this way, we can encapsulate database operations into dll files, and then let the program load the dll flexibly through the configuration file, so that the program will survive.
The flexible use of reflection + factory design patterns can make the entire program easier to expand. A good design can save a lot of time, so that programmers do not have to work overtime every day to change the code.
For more details about reflection technology and configuration file technology, you can read the articles of other great gods and do not explain them in detail here. If you have time, I will open up a new column to explain the basic knowledge.
Now, we are here today.Status Mode
This series will be updated continuously. If you like it, you can pay attention to it and recommend it. Thank you for your support.