Replace the switch Branch (14) and switch branch of the factory with reflection technology

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.