Refactoring in section 6.9Replace method with method object (replace functions with function objects)
Replace method with method object (replace functions with function objects) Is there any experience!
For exampleCode:
When test1 () Test2 () is called in the testmethod () method,
If replace method with method object is appliedProgramCorrectness,
You must change test1 () Test2 () to public method.
Based on this change, the test1 () test () method is exposed. In this case, what are the best suggestions?
1
2 Public Class Testclass
3 {
4 Private Void Test1 (){}
5 Private Void Test2 (){}
6
7 Public Void Testmethod ()
8 {
9 If ( True )
10 {
11 // Several Methods
12 Test1 ();
13 }
14 Else
15 {
16 // Several Methods
17 Test2 ();
18 }
19 }
20 }
21
22
The replace method with method object method is used because the current class contains thousands of lines of code.
This class is the main form of a program. Because there are many menus, there are a lot of Event code. Of course, I have used partial class to separate all the Event code into a separate file.
However, this class still contains more than 500 lines of code. Two methods each contain about 70 lines of code.
So I used replace METHOD WITH METHOD object to separate the two methods.
For these two methods:
The checklogin () method is used to verify the user's logon. In this method: A loginform object is constructed, and a button clicking event is added to the object using the anonymous method.
Changeuser () method to change the login user. The method is the same as above. The difference is that the event code of the object button is different.
About the loginform class:
The checklogin () & changeuser () method shares the loginform form. Therefore, no button clicking event is added in the instantiation method of the loginform class.