Path to learning: Application of generics and delegation in reconstruction

Source: Internet
Author: User

Recently, I have reconstructed the code in the project and found some interesting things in the refactoring process: generics and delegation!

Generics and delegation bring great convenience to my refactoring. I also have some new ideas on their refactoring technologies!

If you say something wrong with the dish, ask the garden friends to help me point out that I will accept it modestly. Thank you ~!

Below I will only talk about some ideas about generics and delegation during refactoring, excluding all the knowledge points about generics and delegation!

1. Generic

Code before reconstruction:

1 public class test 2 {3 Public object testone (INT number) 4 {5 switch (number) // select object 6 {7 case 1 Based on the passed number: 8 return test1 (); 9 Case 2: 10 return Test2 (); 11 default: 12 return NULL; 13} 14} 15 16 public person1 test1 () // very simple, returns an object instance 17 {18 person1 person1 = new person1 (); 19 Return person1; 20} 21 22 public person2 Test2 () // This also returns an object instance 23 {24 person2 person2 = new person2 (); 25 return person2; 26} 27} 28 29 public class person130 {} 31 32 public class person233 {}

Analysis:

① First, we can see that the action of two methods is to return the object instance!

② That is, the operation behavior is similar, but the returned type is different!

③ After finding the similarities, We need to encapsulate the changing points, that is, the changes of the encapsulation type!

③ At last, we can easily think of using generics to achieve such a requirement!

Code after reconstruction:

1 public class test 2 {3 Public object testone (INT number) 4 {5 switch (number) 6 {7 Case 1: 8 return testtwo <person1> (); 9 Case 2: 10 return testtwo <person2> (); 11 default: 12 return NULL; 13} 14} 15 16 public t testtwo <t> () where T: Class, new () // encapsulate the Type Change Point 17 {18 t = new T (); 19 Return T through generics; 20} 21} 22 23 public class person124 {} 25 26 public class person227 {}

Conclusion: Changes in generic encapsulation types

The generic type is used to pass the type, specifying which type should be used now, so that the type encapsulation can be achieved, so as to remain unchanged!

 

Ii. Delegation

Code before reconstruction:

1 public class test 2 {3 Public void test1 () 4 {6 datatable table = new datatable (); // obtain the data source 7 // generate entity 8 foreach (datarow item in table. rows) 9 {10 generateentity1 (item); 11} 12} 13 14 public void Test2 () 15 {17 able table = new datatable (); // obtain the data source 18 // generate entity19 foreach (datarow item in table. rows) 20 {21 generateentity2 (item); 22} 23} 24 25 public void generateentity1 (datarow) 26 {27 // generate entity128} 29 30 public void generateentity2 (datarow) 31 {32 // generate entity233} 34}

Analysis:

① First, we will see that when the entity is generated in the test1 and Test2 methods, there will be repeated foreach loop code!

② To extract the Common Code on both sides, we need to analyze which codes on both sides are the same and which are changed so that we can extract the unchanged code, however, some technologies are used to encapsulate the change points, and the changes will eventually become universal!

③ We can easily find that the foreach loop code is the same, and the entity generated is different, but take a closer look at the parameters of the entity generation method and the return value are the same!

④ How can we encapsulate the frequently changing point?

⑤ We want to delegate and pass the operation behavior through the delegate to achieve changes!

Code after reconstruction:

1 public class Test2 2 {3 Public void test1 () 4 {5 datatable table = new datatable (); // data source 6 generateentity (table, generateentity1 ); 7} 8 9 Public void Test2 () 10 {11 datatable table = new datatable (); // data source, different from the above data source 12 generateentity (table, generateentity2 ); 13} 14 15 public void generateentity1 (datarow) 16 {17 // generate entity118} 19 20 public void generateentity2 (datarow) 21 {22 // generate entity223} 24 25 private void generateentity (datatable table, Action <datarow> action) // pass the operation behavior through delegation 26 {27 foreach (datarow item in table. rows) 28 {29 action (item); 30} 31} 32}

Conclusion: Changes in delegated encapsulation Behavior

The delegated instance is passed to specify the operation behavior that I need to use. If the returned values are different, then the wildcard is added to encapsulate the type change points, finally, it constitutes the comprehensive use of delegation and generics!

 

Summary

In fact, I want to talk about thinking and summing up in practice. In the end, there will be different gains!

 

Synchronized:Personal document Directory Index

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.