Development (ASP.NET program) writes code to the most object-oriented flavor

Keywords: Design Pattern

A few days ago, when moving the house, I picked up the book refactoring -- improving the design of existing code. Refactoring is often and often used in development.



She did teach us how to write programs concise, clear, easy to understand, easy to maintain

Write something about refactoring for the blog today. Take the actual example of netizens and see it on the Forum:

public void show2(string day)
    {
        string[] str = new string[] { "09:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00" };
        for (int i = 0; i < str.Length; i++)
        {
            string sql1 = "select position from cwsz where day='" + day + "' and sjd='" + str[i] + "'";
            var dt1 = db.ExecuteQuery(sql1);
            if (dt1.Rows.Count > 0)
            {
                if (dt1.Rows[0]["position"] != null)
                {
                    if (str[i] == "09:00")
                    {
                        if (dt1.Rows[0]["position"].ToString() == "3")
                            dropnine.SelectedIndex = 1;
                        else if (dt1.Rows[0]["position"].ToString() == "4")
                            dropnine.SelectedIndex = 2;
                        else if (dt1.Rows[0]["position"].ToString() == "5")
                            dropnine.SelectedIndex = 3;
                        else
                            dropnine.SelectedIndex = 4;

                    }
                    if (str[i] == "10:00")
                    {
                        if (dt1.Rows[0]["position"].ToString() == "3")
                            dropten.SelectedIndex = 1;
                        else if (dt1.Rows[0]["position"].ToString() == "4")
                            dropten.SelectedIndex = 2;
                        else if (dt1.Rows[0]["position"].ToString() == "5")
                            dropten.SelectedIndex = 3;
                        else
                            dropten.SelectedIndex = 4;
                    }
                    if (str[i] == "11:00")
                    {
                        if (dt1.Rows[0]["position"].ToString() == "3")
                            dropele.SelectedIndex = 1;
                        else if (dt1.Rows[0]["position"].ToString() == "4")
                            dropele.SelectedIndex = 2;
                        else if (dt1.Rows[0]["position"].ToString() == "5")
                            dropele.SelectedIndex = 3;
                        else
                            dropele.SelectedIndex = 4;
                    }
                    if (str[i] == "12:00")
                    {
                        if (dt1.Rows[0]["position"].ToString() == "3")
                            droptw.SelectedIndex = 1;
                        else if (dt1.Rows[0]["position"].ToString() == "4")
                            droptw.SelectedIndex = 2;
                        else if (dt1.Rows[0]["position"].ToString() == "5")
                            droptw.SelectedIndex = 3;
                        else
                            droptw.SelectedIndex = 4;
                    }
                    if (str[i] == "13:00")
                    {
                        if (dt1.Rows[0]["position"].ToString() == "3")
                            tropthir.SelectedIndex = 1;
                        else if (dt1.Rows[0]["position"].ToString() == "4")
                            tropthir.SelectedIndex = 2;
                        else if (dt1.Rows[0]["position"].ToString() == "5")
                            tropthir.SelectedIndex = 3;
                        else
                            tropthir.SelectedIndex = 4;
                    }
                    if (str[i] == "14:00")
                    {
                        if (dt1.Rows[0]["position"].ToString() == "3")
                            dropfour.SelectedIndex = 1;
                        else if (dt1.Rows[0]["position"].ToString() == "4")
                            dropfour.SelectedIndex = 2;
                        else if (dt1.Rows[0]["position"].ToString() == "5")
                            dropfour.SelectedIndex = 3;
                        else
                            dropfour.SelectedIndex = 4;
                    }
                }
            }
        }
    }


When I first saw this code, I found that the user wrote the same code at a glance, that is, redundancy. Used a lot of if...else. These are the problems that need to be reconstructed mentioned in the book.

Refactoring in several steps. First, solve the code redundancy. Starting from the if innermost layer, Insus.NET has marked out different components. See the figure below:

 

Write the same code into a method. In the future, once the SelectedIndex of the drop-down list is maintained, only this method can be modified. There is no need to check or find replacement in each if.



Then we can modify the extracted code, and Insus.NET deletes the comment of the refactoring part:


Now we delete the comment code and change it to the following. The code is much simpler:


However, after the first refactoring, there are still too many if. If you have studied design patterns, you will naturally think of using these programs< Design pattern -- mediator pattern>http://www.cnblogs.com/insus/p/4134383.html   Just pass in the str[i] value. Users don't have to understand how it is handled and judged to achieve the encapsulation effect:

 
Refactoring using design patterns:


The last private int SetSelectIndex(string position) method, we also moved it to the mediator category:
The code left for the user to see is (delete comment):


For object-oriented programming, the two lines of code highlighted above should come from an object. No matter whether your program uses stored procedures or writes SQL statements, we try not to appear in the xxx.aspx.cs code page:


In this way, in the xxx.aspx.cs code, it can be changed to:

 

Now let's look back at the mediator SwitchObject class. Its bottom private method actually uses the switch method, which is also a bad use in refactoring. In fact, switch is used to replace multiple if judgments. When multiple if occur, we can use the mediator model to solve it.


Create a mediator class:



Let's take a look at how Insus.NET modifies the SwitchObject class:



After refactoring, the whole program has been changed almost, but in the SwitchObject class, some code is very unstable. It may be frequently modified, so we still remove it. Only stable code is left:

 
Insus.NET wants to move it to the original location. Before moving, the Select() method needs to be slightly modified. It is expected to pass in another parameter:


Back to xxx.aspx.cs   public void show2(string day) method:


After refactoring, you still see an if judgment:

 

There seems to be a problem with the change. Correctly, when the incoming position is empty, it is equal to 4. Instead, when the obtained selectedIndex is not equal to 4, it is equal to 4.


After the reconstruction, the final source program can be downloaded from the following link:
http://download.cnblogs.com/insus//Refactoring/RefactoringAndMediator_pattern.rar

 

turn   https://www.cnblogs.com/insus/p/4139346.html

Posted by cx3op on Tue, 09 Nov 2021 16:55:44 -0800