Replace method with method object (replace functions with function objects)

Source: Internet
Author: User

 

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.

 

 

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.