Step by step. NET code refactoring learning notes Series
Html "> step by step. NET code refactoring Study Notes 1. Why code refactoring?
Step by step. NET code refactoring Study Notes II. Extraction Method (Extract Method)
Step by step. NET code refactoring study note 3. Inline Method)
Step by step. NET code refactoring Study Notes 4. Temporary variables (Temporary Variable)
Step by step. NET code refactoring study note 5. decomposition functions And replacement algorithms (Replace Method And Substitute Algorithm)
Step by step. NET code refactoring study note 6. Move Method And Move Field)
Step by step. NET code refactoring study note 7
Step by step. NET code refactoring learning notes 8
Step by step. NET code refactoring study note 9
1. Replace Type Code with Subclasses (Replace Type Code with subclass)
Motivation)
Replace this type code with a subclass. If the type code it faces does not affect the behavior of the host Class, you can use Replace Type Code with Class to process them. However, if the type code affects the behavior of the host class, the best way is to use the polymorphism industry to handle changing behavior.
Example
View sourceprint? 01 public class Employee
02 {
03 private int _ type;
04 public static int ENGINEER = 0;
05 public static int SALEMAN = 1;
06 public static int MANAGER = 2;
07
08 public Employee (int type)
09 {
10 _ type = type;
11}
12}
Change
View sourceprint? 01 public class Employee
02 {
03 private int _ type;
04 public static int ENGINEER = 0;
05 public static int SALEMAN = 1;
06 public static int MANAGER = 2;
07
08 public Employee (int type)
09 {
10 _ type = type;
11}
12
13 public int Type
14 {
15 get {return _ type ;}
16 set {_ type = value ;}
17}
18
19
20}
21
22 public class ENGINEER: Employee
23 {
24 public int GetType ()
25 {
26 return Employee. ENGINEER;
27}
28}
29
30 public class SALEMAN: Employee
31 {
32 public int GetType ()
33 {
34 return Employee. SALEMAN;
35}
36}
37
38 public class MANAGER: Employee
39 {
40 public int GetType ()
41 {
42 return Employee. MANAGER;
43}
44}
45
46 public class Factory: Employee
47 {
48 public Employee Create (int type)
49 {
50 switch (type)
51 {
52 case ENGINEER:
53 return new ENGINEER ();
54 case SALEMAN:
55 return new SALEMAN ();
56 case MANAGER:
57 return new MANAGER ();
58 default:
59 throw new ExecutionEngineException ("Incorrect type code value ");
60}
61}
62}
2. Replace Type Code with State/Strategy (Replace Type Code with State/Strategy)
Motivation)
Replace type code with State object (an object specifically used to describe the State.
Example
View sourceprint? 01 public class Employee
02 {
03 private int _ type;
04 public static int ENGINEER = 0;
05 public static int SALEMAN = 1;
06 public static int MANAGER = 2;
07
08 public Employee (int type)
09 {
10 _ type = type;
11}
12
13 public int Type
14 {
15 get {return _ type ;}
16 set {_ type = value ;}
17}
18
19 public int PayAmount ()
20 {
21 switch (_ type)
22 {
23 case ENGINEER:
24 return 100;
25 case SALEMAN:
26 return 1000;
27 & n